![](/img/trans.png)
[英]I wonder why connection between eclipse and 'oracle database' doesn't work
[英]I wonder why the getBoardList test using the Pageable object of this WebMVCTest doesn't work
有源代码
@WebMvcTest(BoardController.class)
public class BoardControllerTest {
private MockMvc mvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
BoardController boardController;
@MockBean
BoardServiceImpl boardService;
@BeforeEach
public void setUp() {
mvc = MockMvcBuilders
.standaloneSetup(boardController)
.build();
}
@Test
public void getBoardList() throws Exception {
PageRequest pageable = PageRequest.of(0,10, Sort.Direction.DESC, "id");
List<BoardDto> results = new ArrayList<>();
results.add(new BoardDto(1, "Shown Title1", null, "Mingyeom", 2, LocalDateTime.now(), null));
results.add(new BoardDto(2, "Shown Title2", null, "Mingyeom", 2, LocalDateTime.now(), null));
results.add(new BoardDto(3, "Shown Title3", null, "Mingyeom", 2, LocalDateTime.now(), null));
Page<BoardDto> expectedBoardDto = new PageImpl<>(results,pageable, results.size());
given(boardService.getBoardList(anyInt(), any())).willReturn(expectedBoardDto);
// when
String responseBody = mvc.perform(get("/board/all")
.contentType(MediaType.APPLICATION_JSON)
.param("menuId", "2")
.param("page", "2")
.param("size", "1")
.param("sort", "ASC")
.param("properties", "id"))
.andExpect(status().isOk())
.andDo(print())
.andReturn()
.getResponse().getContentAsString();
// then
assertThat(responseBody).isEqualTo(objectMapper.writeValueAsString(expectedBoardDto));
}
}
@RestController
@RequestMapping("/board")
@RequiredArgsConstructor
public class BoardController {
private final BoardService boardService;
@GetMapping("/all")
public Page<BoardDto> getBoardList(
Pageable pageable,
@RequestParam Integer menuId
) {
return boardService.getBoardList(menuId, pageable);
}
}
基于下面的BoardController,我尝试测试了接收可分页的object并接收menuId作为参数检索帖子整篇文章的方法。
在 BoardControllerTest 中创建一个 PageRequest object 后,我创建了一个 BoardDto 列表,并使用 PageImpl 方法创建了一个页面 object。
(对不起,缺少帖子。)
如果你用同样的方法向web上的地址栏发送请求,会正常投递,但如果只是测试一下,就会报错。 以下是错误信息的内容。
请求处理失败; 嵌套异常是 java.lang.IllegalStateException: No primary or single unique constructor found for interface org.springframework.data.domain.Pageable org.springframework.web.util.NestedServletException: 请求处理失败; nested exception is java.lang.IllegalStateException: No primary or single unique constructor found for interface org.springframework.data.domain.Pageable at app//org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at app//org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) at app//javax.servlet.http.HttpServlet.service(HttpServlet.java:655) at app//org.springframework.web .servlet.FrameworkServlet.service(FrameworkServlet.java:883) at app//org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:72) at app//javax.servlet.Z80791B3AE7002CB88C246876D9FAA8F8 Z.HttpServlet.service(HttpServlet.java:764) at app//org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167) at app//org.springframework.mock.web.MockFilterChain. doFilter(MockFilterChain.java:134) at app//org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:199) at app//com.inhabas.api.controller.BoardControllerTest.getBoardList(BoardControllerTest. java:125) at java.base@11.0.12/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base@11.0.12/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62 ) at java.base@11.0.12/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base@11.0.12/java.lang.reflect.Method.invoke(Method.java:566 ) at app//org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725) at app//org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) at app//org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at app//org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) at应用程序//org.Z587FEFE304B8E3505DE89 580432C5DDCZ.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) at app//org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) at app//org.junit. jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) at app//org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) at app //org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at app//org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.Z93F725A0 7423FE1C889F448B33D21F46Z:64) at app//org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at app//org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java: 37) at app//org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) at app//org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) at app//org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214) at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(Throwable Collector.java:73) at app//org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210) at app//org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor. java:135) at app//org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66) at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6 (NodeTestTask.java:151) at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at app//org.junit.platform.engine.support.hierarchical .NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at app//org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at app//org.junit.platform. engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at app//org .junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java. base@11.0.12/java.util.Z57A97A39435CFDFED96E03F2A3BC2 7CEZ.forEach(ArrayList.java:1541) at app//org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at app//org.junit.platform.engine.support. hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at app//org.junit.platform .engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at app//org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at app// org.junit.platfo rm.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at app//org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at app/ /org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at app//org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base@11.0.12/java.util.ArrayList.forEach(ArrayList.java:1541)
请帮忙,, :(
我解决了!
@BeforeEach
public void setUp() {
mvc = MockMvcBuilders
.standaloneSetup(boardController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.setViewResolvers(new ViewResolver() {
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return new MappingJackson2JsonView();
}
})
.build();
}
我在 BoardControllerTest 之上编写了这段代码,它编译成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.