```java
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
@EnableAutoConfiguration(exclude = {WebMvcAutoConfiguration.class})
public class DemoControllerTest {
@Autowired
private MockMvc mvc;
@Autowired
private WebApplicationContext context;
@Before
public void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(context).dispatchOptions(true).build();
}
@Test
public void getApplicationVersionTest() throws Exception {
mvc.perform(get("/demo/version").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
Я обновил версию Springboot с v2.1.7 до v2.2.5, и с тех пор мои тесты контроллера не пройдены, потому что макет возвращает 404 для всех. Я исключил класс WebMvcAutoConfiguration
из-за ошибки при создании объекта. Теперь макет возвращает 404 для всех.
Error Trace:
java.lang.AssertionError: Status expected:<200> but was:<404>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:627)
at org.springframework.test.web.servlet.result.StatusResultMatchers$$Lambda$879.0000000014CC94F0.match(Unknown Source)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
at com.demo.controller.ControllerTest.getApplicationVersionTest(ControllerTest.java:67)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
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 org.junit.vintage.engine.VintageTestEngine$$Lambda$249.0000000011B25800.accept(Unknown Source)
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:496)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:486)
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:241)
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$$Lambda$209.0000000011A9CB80.accept(Unknown Source)
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:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
```
I have upgraded my Spring Boot version from v2.1.7 to v2.2.5 and since then my controller tests are failing because the mock is returning 404 for all. I have excluded `WebMvcAutoConfiguration` class for bean instantiation error. Now the mock returns 404 for all.
Below is the my controller class:
@PropertySource(ignoreResourceNotFound = true, value = "classpath:app-version.properties")
@RestController
@RequestMapping("/demo")
public class DemoController extends CommonController {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoController.class);
private DemoService demoService;
@Value("${environment.name:unknown}")
private String environmentName;
@Value("${buildNumber:unknown}")
private String buildNumber;
private EovService eovService;
@Autowired
public DemoController(DemoService demoService, EovService eovService) {
this.demoService = demoService;
this.eovSqsService = eovSqsService;
}
@GetMapping(value = "/version", produces = {"application/json"}, consumes = {
"application/json"})
public ResponseEntity getApplicationVersionNumber() {
String result = environmentName + " " + buildNumber;
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
Below is what I receive in my console for mockServletResponse. The controller class is starting as expected but the mockMvc is receiving 404 response for all the requests.
12:09:52.247 [] [scheduling-1] INFO
c.t.r.c.h.HeartbeatBatchScheduler - [HEALTH_CHECK][RTCA][svc-demo-status][LOCAL][UP]
12:09:52.254 [] [main] INFO
c.t.r.c.n.c.DemoControllerTest - Started DemoControllerTest in 12.598 seconds (JVM running for 14.864)
12:09:52.398 [] [main] DEBUG
c.t.r.s.j.s.AbstractJWTCommonFilter - In DoFilter JWT Authentication class
MockHttpServletRequest:
HTTP Method = GET
Request URI = /demo/version
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8"]
Body = null
Session Attrs = {}
Handler:
Type = null
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 404
Error message = null
Headers = []
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
12:09:52.468 [] [SpringContextShutdownHook] DEBUG
o.a.h.i.c.PoolingHttpClientConnectionManager - Connection manager is shutting down
12:09:52.469 [] [SpringContextShutdownHook] DEBUG
o.a.h.i.c.PoolingHttpClientConnectionManager - Connection manager shut down