JUnit & Spring – What You Don’t Know
Join the DZone community and get the full member experience.
Join For FreeFirst, if you are including the Spring Context in your tests, it becomes an Integration Test, no longer a Unit Test.
1. Default Searching of Context File(s)
To instruct Spring to load beans for the tests in the class, we annotate the class with @ContextConfiguration
1a. No File Specified
@ContextConfiguration – with no parameters, (by Default) looks for the config file as the same name as the class with the suffix “-context.xml“. For example,
Using
package com.gordondickens.sample;
@ContextConfiguration
public class UtilsTest {
...
}
...
Is equivalent to
...Will look for a file called UtilsTest-context.xml in the Classpath under the same package as the test. If we are using the standard Maven structure, Spring will search for:
@ContextConfiguration("classpath:/com/gordondickens/sample/UtilsTest-context.xml")
...
- src/test/java/com/gordondickens/sample/UtilsTest-context.xml
- src/test/resources/com/gordondickens/sample/UtilsTest-context.xml
- src/main/java/com/gordondickens/sample/UtilsTest-context.xml
- src/main/resources/com/gordondickens/sample/UtilsTest-context.xml
1b. File specified (without a starting Slash)
Using
package com.gordondickens.sample;
@ContextConfiguration("utils-context.xml")
public class UtilsTest {
...
}
...
Is equivalent to
... |
Spring searches for utils-context.xml in each classpath’s directory for package and does NOT traverse subdirectories (packages).
1c. File specified with a Starting Slash
One Simple change can ruin your whole day! Add a Starting Slash to the file name.
Using
...
@ContextConfiguration(value = "/META-INF/spring/utils-context.xml")
...
is equivalent to
...
@ContextConfiguration("classpath:/META-INF/spring/utils-context.xml")
...
1d. Multiple Files
Pulling multiple configuration files into the application context for your tests.
...
@ContextConfiguration(locations = {"utils-context.xml", "app-context.xml"})
...
Best Practice Tip
Create an XML file per test that imports only the application’s context files that are needed.
This can save test execution time, where we only load beans necessary for these tests.
2. Spring Aware Test Options
We can load configuration files for the tests, but we want the added benefit of using Spring Annotations.
NOTE: This requires JUnit 4.5 or later.
Annotate the test class with @RunWith(SpringJUnit4ClassRunner.class).
2a. Autowiring Beans
@Autowired | Autowire (inject) in dependencies |
Tip: If you want access to the ApplicationContext you can simply autowire this into your test class.
...
@Autowired
ApplicationContext applicationContext;
...
2b. Transactional Test Methods
@Transactional | At the method level, method is transactional. Class level, all methods are transactional |
@TransactionConfiguration | Define default transaction parameters for the class |
@Rollback | Change rollback settings for methods |
@BeforeTransaction | Method to execute before a transaction |
@AfterTransaction | Method to execute after a transaction |
NOTE: Transactions are rolled back by default in tests. Allowing repeat execution to perform the same db actions.
2c. Profiles - Evaluating Environment
@ProfileValueSourceConfiguration | Class level settings for all tests, defaults to system properties |
@IfProfileValue | Specify name and value(s) that must match to execute the test method |
2d. Timeout, Repeating & Invalidating Context
@Timed | Defined maximum time that method has to execute |
@Repeat | Specify the number of times the test method will repeat |
@DirtiesContext | At class or method level, mark the app context as dirty and should be closed. The app context will be recreated for subsequent test |
From http://gordondickens.com/wordpress/2011/01/07/junit-spring-what-you-dont-know-about/
Opinions expressed by DZone contributors are their own.
Comments