【发布时间】:2022-09-26 00:39:40
【问题描述】:
我有一个控制器,它接受名为 \'jobName\' 的路径参数。 @ValidateJobName 是验证用户输入的自定义验证器。如果输入错误,则会引发以下错误
\"无效的作业名称,有效的作业名称为:供应商、服务、产品、 定价、货币、合约\"
我面临的问题是,当我测试我的休息控制器 API 时,即使作业名称是可接受的值之一,但当我从控制器中删除 @ValidateJobName 自定义注释时,测试用例总是会返回上述错误而失败,我的测试案件通过。
当从 Postman 客户端触发时,@ValidateJobName 和 Controller 工作得很好,但是当我进行单元测试时,测试用例失败了。
我尝试了很多博客和谷歌搜索但无法找到解决方案,下面是我的控制器和 JUnit 测试用例。
请帮忙!
作业控制器.java
@Validated @Slf4j @RestController public class JobController { @Autowired ReportService reportService; @Autowired ReportConfig reportConfig; @RequestMapping(value = \"/importjob/{jobName}\", method = RequestMethod.GET) ResponseEntity<DataIntegrationResponse> getReport(@PathVariable @ValidateJobName String jobName) throws Exception { log.info(\"Received a request to launch the \" + jobName + \" Job\"); return reportService.getReport(jobName); } }JobControllerTest.java
@ExtendWith(MockitoExtension.class) @WebMvcTest(JobController.class) @AutoConfigureMockMvc public class JobControllerTest { @MockBean ReportService reportService; @MockBean ReportConfig rep; @MockBean JobMapping jmap; @Autowired public MockMvc mockMvc; @Test public void testGetReport() throws Exception { String jobNameInput=\"vendor\"; HttpStatus httpStatus = HttpStatus.OK; String fitsReportName = \"idex_fits_vendor.csv\"; String jobName = \"WFitsVendorJob\"; String jobStatus = \"STARTED\"; Long jobInstanceId = 1022L; String message = \"WFitsVendorJob triggered successfully.\"; DataIntegrationResponse response = new DataIntegrationResponse(LocalDateTime.now(), httpStatus, fitsReportName, jobName, jobStatus, jobInstanceId, message); ResponseEntity<DataIntegrationResponse> responseEntity = new ResponseEntity<DataIntegrationResponse>(response, HttpStatus.OK); Mockito.when(reportService.getReport(jobNameInput)).thenReturn(responseEntity); mockMvc.perform(get(\"/importjob/{jobName}\", \"vendor\")).andExpect(status().isOk()); }JobNameValidator.java
@Component public class JobNameValidator implements ConstraintValidator<ValidateJobName, String>{ @Autowired private JobMapping jobMap; @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (value!=null && !jobMap.getMappings().containsKey(value)) { return false; } return true; } }ValidateJobName.java - 接口
@Documented @Constraint(validatedBy = JobNameValidator.class) @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) @Retention(RUNTIME) public @interface ValidateJobName { String message() default \"Invalid Job name, valid job names are: vendor, service, product, pricing, currency, contract\"; Class<?>[] groups() default {}; Class<? extends Payload> [] payload() default {}; }
-
我认为您需要启动所有 spring 上下文来执行此操作。在您的测试类中添加一个用
@Before注释的方法,您可以调用任何类似setUp()的方法。在您的班级org.springframework.web.context.WebApplicationContext context;中添加一个依赖项。在setUp()方法中,添加:this.mockMvc = MockMvcBuilders.webAppContextSetup(context) .build();并将其添加到测试类的顶部@RunWith(SpringRunner.class)。 -
@VitorNilson 我尝试了您的解决方案,但没有运气。
标签: rest junit mockito junit5 spring-validator