May 2015
Intermediate to advanced
234 pages
4h 18m
English
JUnit tests are run outside Spring; Spring is not initialized before the tests are run. To be able to use the beans defined in the configuration files and dependency injection, some bootstrapping code needs to be added to the test class.
Follow these steps to test a method using the Spring's application context with JUnit 4:
spring-test Maven dependency in pom.xml:<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.1.RELEASE</version> <scope>test</scope> </dependency>
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {AppConfig.class}) @WebAppConfiguration ...Read now
Unlock full access