I am trying to create JUnit test cases for Spring MVC controllers using junit & spring-test api. Since I have lot of beandefinitions in my app, I used LazyInitDefaultBeanDefinitionDocumentReader
and have written a CustomContextLoader
.
My sample test class would be:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=com.xyz.CustomXmlContextLoader.class,
locations={"file:///D:/web-module/src/test/resources/conf/application-config-controller-test.xml"})
@WebAppConfiguration
public class LoginControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Test
public void testShowForm_forgetUserID() throws Exception {
System.out.println("webappcontext::"+wac);
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
mockMvc.perform(get("/login.form")).andExpect(status().isOk());
}
}
Here, if I execute the above code, wac is not autowired and it returns null. But if I remove loader=com.xyz.CustomContextLoader
from @ContextConfiguration
, it works fine.
I need to have both lazy load and MockMvc for testing. Am I missing anything? Is there any better solution?