java - 没有使用 mockmvc 的请求映射
问题描述
当我使用以下控制器/测试配置收到“请求的映射错误”时,目前正在努力解决问题。控制器:
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
public class AdtechController {
private final AdtechService adtechService;
@PostMapping(value = "/subscriber/session")
public ResponseEntity<ResponseDto> submitSession(@RequestBody RequestDto requestDto) {
log.trace("execute submitSession with {}", requestDto);
ResponseDtoresponse = adtechService.submitSession(requestDto);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@ExceptionHandler(AdtechServiceException.class)
public ResponseEntity<AdtechErrorResponse> handleAdtechServiceException(AdtechServiceException e) {
return new ResponseEntity<>(new AdtechErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
测试:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@SpringJUnitConfig({AdtechTestConfig.class})
public class AdtechControllerTest {
private static final ObjectMapper OBJECT_MAPPER = JsonUtil.getJackson();
@Autowired
private MockMvc mockMvc;
@Test
public void testSubmitSession() throws Exception {
RequestDto requestDto = new RequestDto ();
requestDto.setKyivstarId("1123134");
requestDto.setMsisdn("123476345242");
requestDto.setPartnerId("112432523");
requestDto.setPartnerName("125798756");
String request = OBJECT_MAPPER.writeValueAsString(requestDto);
System.out.println("REQUEST: " + request);
String response = OBJECT_MAPPER.writeValueAsString(new ResponseDto("123"));
System.out.println("RESPONSE: " + response);
mockMvc.perform(post("/subscriber/session")
.content(MediaType.APPLICATION_JSON_VALUE)
.content(request))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString(response)));
}
}
配置:
@Configuration
public class AdtechTestConfig {
@Bean
public AdtechService adtechTestService() {
return requestDto -> new AdtechResponseDto("123");
}
}
测试执行后,我得到No mapping for POST /subscriber/session
挣扎的原因是我来自具有相同配置的其他模块的代码工作正常。有人能指出我错过了什么吗?提前致谢!
解决方案
显然您正在加载一个配置类来模拟 bean,这会干扰 Spring Boot 的其他部分,并可能导致部分加载您的应用程序。我怀疑只有模拟服务可用。
代替测试配置@MockBean
用于为服务创建模拟并在其上注册行为。
@SpringBootTest
@AutoConfigureMockMvc
public class AdtechControllerTest {
private static final ObjectMapper OBJECT_MAPPER = JsonUtil.getJackson();
@Autowired
private MockMvc mockMvc;
@MockBean
private AdtechService mockService;
@BeforeEach
public void setUp() {
when(mockService.yourMethod(any()).thenReturn(new AdtechResponseDto("123"));
}
@Test
public void testSubmitSession() throws Exception {
// Your original test method
}
}
如果您只想测试您的控制器,您可能还需要考虑使用@WebMvcTest
而不是@SpringBootTest
.
@WebMvcTest(AdTechController.class)
public class AdtechControllerTest {
private static final ObjectMapper OBJECT_MAPPER = JsonUtil.getJackson();
@Autowired
private MockMvc mockMvc;
@MockBean
private AdtechService mockService;
@BeforeEach
public void setUp() {
when(mockService.yourMethod(any()).thenReturn(new AdtechResponseDto("123"));
}
@Test
public void testSubmitSession() throws Exception {
// Your original test method
}
}
这将加载上下文的缩小版本(仅 Web 部件)并且运行速度会更快。
推荐阅读
- node.js - Create React App 无法在 docker 内部构建
- javascript - 水平滑块错误 - 多个具有相同类的水平滑块与本机 JavaScript ,所有滑块一起工作我如何解决它
- firebase - 在 GCP Console 中创建存储分区后无法访问 Firebase 存储
- python - 如何从“子进程”流式输出(一次一行)?
- git - 为什么我无法克隆“致命错误:服务器意外关闭网络连接”?
- mysql - 使用 Laravel 4.2 通过分页提高多对多查询性能
- mysql - 当我的数据库是 MySQL 时,如何在我的连接字符串中设置 connection_limit 和 pool_timeout?
- rust - `impl Trait` 类型的返回值如何借用检查?
- javascript - 如何在预检请求中强制进行身份验证?
- python - 在 django 中从 pytesseract 自动创建 PDF 文件