Spring MockMvc WebApplicationContext is null when running JUnit Tests with Maven

I have a Spring mock-mvc JUnit testing class that contains two tests. When I run tests inside the Eclipse IDE , both tests pass (I use the Eclipse Maven plugin).

When running tests from the command line using

mvn test 

one of the tests fails because WebApplicationContext, which is @Autowired, is sometimes null.

Here is my test class

 @WebAppConfiguration @ActiveProfiles({ "dev", "test" }) public class AddLinkEndpointMvcTest extends BaseMvc { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void before() { System.out.println("WAC = " + wac); mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void addLinkDoesNotSupportGet() throws Exception { mockMvc.perform(get("/myurl")).andExpect(status().is(HttpStatus.SC_METHOD_NOT_ALLOWED)); } @Test public void addLinkBadRequestNoLinkAddress() throws Exception { mockMvc.perform(post("/myurl")).andExpect(status().is(HttpStatus.SC_BAD_REQUEST)); } } 

Here is the BaseMvc class

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class BaseMvc { @Configuration @ComponentScan(basePackages = { "com.example.a", "com.example.b" }) @Profile("test") public static class TestConfig { static { System.out.println("TEST CONFIG"); } // ...some beans defined here @Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setIgnoreUnresolvablePlaceholders(false); configurer.setLocations(new Resource[] { new ClassPathResource("sample-webapp.properties"), new ClassPathResource("sample-domain.properties") }); return configurer; } } } 

I added println calls for debugging helpers. When running mvn test, here is the corresponding console output:

 WAC = null WAC = org.s pringframework.web.context.support.GenericWebApplicationContext@ 38795184: startup date [Thu Sep 19 16:24:22 BST 2013]; root of context hierarchy 

and mistake

 java.lang.IllegalArgumentException: WebApplicationContext is required at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder.<init>(DefaultMockMvcBuilder.java:66) at org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup(MockMvcBuilders.java:46) at com.mypackage.AddLinkEndpointMvcTest.before(AddLinkEndpointMvcTest.java:31) 

So this is the problem string in the test

 @Autowired private WebApplicationContext wac; 

Sometimes wac is null or did not complete initialization before JUnit @Before flinches.

I do not understand that WebApplicationContext is sometimes null and why it passes in the Eclipse IDE!

+6
source share
2 answers

Try using getBean instead of auto wiring. This should ensure that the WebApplicationContext initialized when accessing it.

Example:

 MyClass myClass = applicationContext.getBean("myClass"); 
0
source

Please use the @WebAppConfiguration annotation. Your question has already been answered: WebApplicationContext does not auto-install

-1
source

Source: https://habr.com/ru/post/954183/


All Articles