How to unit test a Spring MVC controller using @PathVariable?

I’d call what you’re after an integration test based on the terminology in the Spring reference manual. How about doing something like: import static org.springframework.test.web.ModelAndViewAssert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({/* include live config here e.g. “file:web/WEB-INF/application-context.xml”, “file:web/WEB-INF/dispatcher-servlet.xml” */}) public class MyControllerIntegrationTest { @Inject private ApplicationContext applicationContext; private MockHttpServletRequest request; private MockHttpServletResponse response; private HandlerAdapter handlerAdapter; private MyController controller; … Read more

request scoped beans in spring testing

Solution for Spring 3.2 or newer Spring starting with version 3.2 provides support for session/request scoped beans for integration testing. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfig.class) @WebAppConfiguration public class SampleTest { @Autowired WebApplicationContext wac; @Autowired MockHttpServletRequest request; @Autowired MockHttpSession session; @Autowired MySessionBean mySessionBean; @Autowired MyRequestBean myRequestBean; @Test public void requestScope() throws Exception { assertThat(myRequestBean) .isSameAs(request.getAttribute(“myRequestBean”)); assertThat(myRequestBean) .isSameAs(wac.getBean(“myRequestBean”, … Read more

JUnit tests pass in Eclipse but fail in Maven Surefire

I had the same problem (JUnit tests failed in Maven Surefire but passed in Eclipse) and managed to solve it by setting forkMode to always in the maven surefire configuration in pom.xml: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12</version> <configuration> <forkMode>always</forkMode> </configuration> </plugin> Surefire parameters: http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html Edit (January 2014): As Peter Perháč pointed out, the forkMode parameter is … Read more

Rollback transaction after @Test

Just add @Transactional annotation on top of your test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {“testContext.xml”}) @Transactional public class StudentSystemTest { By default Spring will start a new transaction surrounding your test method and @Before/@After callbacks, rolling back at the end. It works by default, it’s enough to have some transaction manager in the context. From: 10.3.5.4 Transaction … Read more

Reuse spring application context across junit test classes

Yes, this is perfectly possible. All you have to do is to use the same locations attribute in your test classes: @ContextConfiguration(locations = “classpath:test-context.xml”) Spring caches application contexts by locations attribute so if the same locations appears for the second time, Spring uses the same context rather than creating a new one. I wrote an … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)