- Saved searches
- Use saved searches to filter your results more quickly
- Spock Framework does not inject WebApplicationContext (Spring MVC Test) [SPR-13707] #18282
- Spock Framework does not inject WebApplicationContext (Spring MVC Test) [SPR-13707] #18282
- Comments
- java.lang.IllegalArgumentException WebApplicationContext is required SpringRunner.class
- Root Cause
- How to Reproduce this issue
- Solution 1
- Solution 2
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spock Framework does not inject WebApplicationContext (Spring MVC Test) [SPR-13707] #18282
Spock Framework does not inject WebApplicationContext (Spring MVC Test) [SPR-13707] #18282
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don’t feel is valid
Comments
Manuel Jordan opened SPR-13707 and commented
I have a project with the following:
@WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes=RootApplicationContextConfig.class,ServletApplicationContextConfig.class>) @SuppressWarnings("deprecation") public class _01_PersonaXmlFindOneControllerTest extends PersonaControllerSupport < private static final Logger logger = LoggerFactory.getLogger(_01_PersonaXmlFindOneControllerTest.class); @Rule public TestName name = new TestName(); @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before public void setUp() < mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); >
it works fine for any @Test method.
Now, I am trying to integrate Spock
@WebAppConfiguration @ContextConfiguration(classes=[RootApplicationContextConfig.class,ServletApplicationContextConfig.class]) class PersonaXmlFindOneControllerTest extends Specification < @Autowired private WebApplicationContext webApplicationContext; def MockMvc mockMvc; def setup()< mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); > def "uno"()< expect: true > >
com.manuel.jordan.controller.xml.PersonaXmlFindOneControllerTest > uno FAILED java.lang.IllegalArgumentException at PersonaXmlFindOneControllerTest.groovy:31 … The following problems occurred: java.lang.IllegalArgumentException: WebApplicationContext is required
The problem is Spock is not able to retrieve and inject the WebApplicationContext.
I did a research on Google, seems Spring Boot resolves this through @IntegrationTest …
but I want to know the solution for Spring Core. What is missing about configuration? Is there a special annotation?
Affects: 4.2 GA, 4.2.2
The text was updated successfully, but these errors were encountered:
java.lang.IllegalArgumentException WebApplicationContext is required SpringRunner.class
In this post, we will explore the issue of loading the WebApplicationContext. The common issue is not loading the WebApplicationContext into the spring boot test environment. WebApplicationContext provides a web-based environment to run test cases.
The WebApplicationContext is not required to run a spring boot console test. The web-based spring boot application requires WebApplicationContext to run the Junit test cases.
java.lang.IllegalArgumentException: WebApplicationContext is required at org.springframework.util.Assert.notNull(Assert.java:198) at org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder.(DefaultMockMvcBuilder.java:52) at org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup(MockMvcBuilders.java:51) at com.test.StudentTestController.setup(StudentTestController.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:40) at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184) at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) at java.util.Iterator.forEachRemaining(Iterator.java:116) at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80) at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:71) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229) at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197) at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137) at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Root Cause
The root cause of this exception is not to load WebApplicationContext in the spring boot test environment. WebApplicationContext is required to execute any web-based test request in the web-based spring boot application. This issue is due either to a web-based environment that is not available or to a WebApplicationContext that is not created properly
How to Reproduce this issue
There are two ways to replicate this issue. This issue will be reproduced in the code below.
package com.yawintutor; import org.junit.Before; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @SpringBootTest public class StudentTestController < @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before public void setup() < mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); >>
Solution 1
Using @RunWith(SpringRunner.class) in the spring boot test classes that connect JUnit library to Spring Boot Test Environment. SpringJUnit4ClassRunner is the main test class on Junit. SpringRunner is an alias of SpringJUnit4ClassRunner class
To load the WebApplicationContext, the spring boot test class must have annotation @RunWith(SpringRunner.class) before the class. If the annotation is not added in your test case, add the annotation as shown in the below example.
@SpringBootTest @RunWith(SpringRunner.class) public class StudentTestController< @Autowired private WebApplicationContext webApplicationContext;
Solution 2
The WebApplicationContext variable must have annotation with @Autowired. Otherwise, WebApplicationContext may not load in the test environment. The below code explain the WebApplicationContext.
package com.yawintutor; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @SpringBootTest @RunWith(SpringRunner.class) public class StudentTestController < @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before public void setup() < mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); >>