#spring-mvc-test
#spring-mvc-тест #spring-mvc-test
Вопрос:
Я пишу небольшой REST API с spring boot и пытаюсь написать тест MVC.
К сожалению, тесты MVC завершаются неудачей, так как я получаю пустой ответ от контроллера.
Класс контроллера:
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping
public ResponseEntity getMessage(){
ResponseEntity responseEntity = null;
try {
responseEntity = new ResponseEntity("Hello from: " InetAddress.getLocalHost().getHostAddress(), HttpStatus.OK);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return responseEntity;
}
}
Метод тестирования MVC:
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/test")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello from:")));
}
Проблема в том, что я получаю content()
всегда пустой.
Я попытался напечатать ответ, как показано ниже
MvcResult result = this.mockMvc.perform(get("/test")).andReturn();
String res = result.getResponse().getContentAsString();
в котором четко указано, что res
пусто.
У меня также есть тест mockito, который отлично работает при правильном ответе.
Тест Mockito:
@Test
public void greetingShouldReturnMessageFromService() throws Exception {
when(service.getMessage()).thenReturn(ResponseEntity.ok("Hello from:"));
}
Выходные данные для теста MVC:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /test
Parameters = {}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = com.test.rest.controller.TestController$MockitoMock$625340695
Method = com.test.rest.controller.TestController$MockitoMock$625340695#getMessage()
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = []
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
MockHttpServletRequest:
HTTP Method = GET
Request URI = /test
Parameters = {}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = com.test.rest.controller.TestController$MockitoMock$625340695
Method = com.test.rest.controller.TestController$MockitoMock$625340695#getMessage()
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = []
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
MockHttpServletRequest:
HTTP Method = GET
Request URI = /test
Parameters = {}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = com.test.rest.controller.TestController$MockitoMock$625340695
Method = com.test.rest.controller.TestController$MockitoMock$625340695#getMessage()
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = []
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Response content
Expected: a string containing "Hello from:"
but: was ""
Expected :a string containing "Hello from:"
Actual :""
<Click to see difference>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
at org.springframework.test.web.servlet.result.ContentResultMatchers.lambda$string$3(ContentResultMatchers.java:129)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
at com.test.rest.TestControllerMockTest.shouldReturnDefaultMessage(TestControllerMockTest.java:38)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
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:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Ответ №1:
Вы пробовали @RequestMapping на уровне метода?
Пример:
@RequestMapping("/hello") //Changed here
public ResponseEntity getMessage(){
ResponseEntity responseEntity = null;
try {
responseEntity = new ResponseEntity("Hello from: " InetAddress.getLocalHost().getHostAddress(), HttpStatus.OK);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return responseEntity;
}
Вам нужно будет изменить URL, к которому вы сейчас обращаетесь, на «/ test / hello».
Комментарии:
1. Я думаю, это не должно повлиять на проблему
2. Проверено, как предложено … все та же проблема
Ответ №2:
Я не уверен, но после изменения метода, как показано ниже, он сработал.
@Test
public void shouldReturnDefaultMessage() throws Exception {
when(service.getMessage()).thenReturn(ResponseEntity.ok("Hello from:")); // Added this line
this.mockMvc.perform(get("/test")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello from:")));
}
В приведенном выше методе я добавил when(service.getMessage()).thenReturn(ResponseEntity.ok("Hello from:"));
строку, и тест пройден.
Ссылка: https://spring.io/guides/gs/testing-web /
В конце этой страницы указано, что:
Мы используем @MockBean для создания и внедрения макета для GreetingService (если вы этого не сделаете, контекст приложения не сможет запуститься), и мы устанавливаем его ожидания с помощью Mockito
.