WebApplicationContext does not auto-install

I am writing this test class:

@ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" }) public class CandidateControllerTest { @Mock(name = "candidateService") private CandidateService candidateService; @InjectMocks private CandidateMenuController candidateMenuController = new CandidateMenuController(); @Autowired WebApplicationContext wac; MockMvc mockMvc; @Before public void before() { mockMvc = MockMvcBuilders.webApplicationContextSetup(wac).build(); } } 

But:

After executing the code, I see the following trace:

 java.lang.IllegalArgumentException: WebApplicationContext is required at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.test.web.server.setup.InitializedContextMockMvcBuilder.<init>(InitializedContextMockMvcBuilder.java:39) at org.springframework.test.web.server.setup.MockMvcBuilders.webApplicationContextSetup(MockMvcBuilders.java:73) at controllers.CandidateControllerTest.before(CandidateControllerTest.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

What would I do to fix my problem?

UPDATE

I am changing the code:

 @ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" }) @WebAppConfiguration public class CandidateControllerTest { @Mock(name = "candidateService") private CandidateService candidateService; @InjectMocks private CandidateMenuController candidateMenuController = new CandidateMenuController(); @Autowired WebApplicationContext wac; MockMvc mockMvc; @Before public void before() { MockitoAnnotations.initMocks(this); // this.mockMvc = // MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build(); mockMvc = MockMvcBuilders.webApplicationContextSetup(wac).build(); } ... } 

track:

 java.lang.IllegalArgumentException: WebApplicationContext is required at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.test.web.server.setup.InitializedContextMockMvcBuilder.<init>(InitializedContextMockMvcBuilder.java:39) at org.springframework.test.web.server.setup.MockMvcBuilders.webApplicationContextSetup(MockMvcBuilders.java:73) at controllers.CandidateControllerTest.before(CandidateControllerTest.java:61) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
+6
source share
5 answers

There is no WebApplicationContext only ApplicationContext unless you add @WebAppConfiguration to the test class.

 @ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" }) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class CandidateControllerTest { ... } 

Instead of annotating @RunWith you can also extend one of the convenience classes of springs.

 @ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" }) @WebAppConfiguration public class CandidateControllerTest extends AbstractJUnit4SpringContextTests { ... } 

References

+11
source

I had the same issue using TestNG and Mockito.

Turns off wac is not auto-installed and is available in @BeforeTest methods, but is in @Test methods.

I moved this

 mockMvc = MockMvcBuilders.webApplicationContextSetup(wac).build(); 

to @Test and presto method, it works!

Here is the link I found with the solution: http://forum.spring.io/forum/spring-projects/web/737624-problem-with-autowiring-webapplicationcontext-with-annotationconfigcontextloader

+9
source

WebApplicationContext is required and NullPointerException are the most confusing errors that I encountered as new to TestNG and Spring Test Framework. This is due to simple errors, such as β€œforgetting” about the AbstractTestNGSpringContextTests1 extension, etc. To avoid these errors, I will give you the code template that I use.

 @Test @WebAppConfiguration @ContextConfiguration(classes = WebConfig.class) //You can use your xml too public class YourControllerTest extends AbstractTestNGSpringContextTests { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Test public void getEmailVerificationTest() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); this.mockMvc.perform(get("/home") .accept(MediaType.ALL)) .andExpect(status().isOk()) .andExpect(view().name("home/index")); } } 

This is sample code for checking the home page. If you are new, an error occurs, for example, mentioned above, first check if extends AbstractTestNGSpringContextTests and this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); in appropriate places.

Another thing, you can use the @BeforeMethod annotation to stop repeating this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); in each module. You should add @BeforeMethod as shown below.

  @BeforeMethod public void setWac(){ this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } 
+1
source

Add the annotation below for your installation. @BeforeClass (dependsOnMethods = {"springTestContextPrepareTestInstance"})

fooobar.com/questions/123303 / ... by romeara

0
source

try to do it

 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import com.javainuse.test.SpringBootHelloWorldTests; public class TestWebApp extends SpringBootHelloWorldTests { @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before public void setup() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void testEmployee() throws Exception { mockMvc.perform(get("/employee")).andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.name").value("emp1")).andExpect(jsonPath("$.designation").value("manager")) .andExpect(jsonPath("$.empId").value("1")).andExpect(jsonPath("$.salary").value(3000)); } } 

and SpringBootHelloWorldTests as below

 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.javainuse.SpringBootHelloWorldApplication; @RunWith(SpringRunner.class) @SpringBootTest(classes=SpringBootHelloWorldApplication.class) public class SpringBootHelloWorldTests { @Test public void contextLoads() { } } 
0
source

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


All Articles