当我依赖 Spring Validator 时,如何使用 MockMvc 测试我的 SpringBoot RestController?
How can I test my SpringBoot RestController using a MockMvc when I rely on a Spring Validator?
在我的休息控制器中,我正在使用自定义 Spring 验证器 class 验证输入 JSON。
当我现在想为控制器编写单元测试时,出现无法找到我的验证器的错误...
我正在为其余控制器中的两个组件使用构造函数注入。
@Autowired
public JobController(JobValidator jobValidator, JobService jobService) {
this.jobValidator = jobValidator;
this.jobService = jobService;
}
这里是我对应的测试class。
@RunWith(SpringRunner.class)
@WebMvcTest(JobsController.class)
@AutoConfigureMockMvc
public class MailMonitorJobControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private JobService jobService;
@Test
public void givenJobs_whenGetJobs_thenReturnJsonArray() throws Exception {
Job job = new Job("TEST");
List<Job> allJobs = Arrays.asList(job);
Mockito.when(jobService.getAllJobs()).thenReturn(allJobs);
mockMvc.perform(get("/api/v1/test")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
感谢任何帮助、提示或建议!
非常感谢@pvpkiran!我还必须将 JobValidator 添加为 Mock!
@RunWith(SpringRunner.class)
@WebMvcTest(JobsController.class)
@AutoConfigureMockMvc
public class MailMonitorJobControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private JobService jobService;
@MockBean
private JobValidator jobValidator;
@Test
public void givenJobs_whenGetJobs_thenReturnJsonArray() throws Exception {
Job job = new Job("TEST");
List<Job> allJobs = Arrays.asList(job);
Mockito.when(jobService.getAllJobs()).thenReturn(allJobs);
mockMvc.perform(get("/api/v1/test")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
在我的休息控制器中,我正在使用自定义 Spring 验证器 class 验证输入 JSON。
当我现在想为控制器编写单元测试时,出现无法找到我的验证器的错误...
我正在为其余控制器中的两个组件使用构造函数注入。
@Autowired
public JobController(JobValidator jobValidator, JobService jobService) {
this.jobValidator = jobValidator;
this.jobService = jobService;
}
这里是我对应的测试class。
@RunWith(SpringRunner.class)
@WebMvcTest(JobsController.class)
@AutoConfigureMockMvc
public class MailMonitorJobControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private JobService jobService;
@Test
public void givenJobs_whenGetJobs_thenReturnJsonArray() throws Exception {
Job job = new Job("TEST");
List<Job> allJobs = Arrays.asList(job);
Mockito.when(jobService.getAllJobs()).thenReturn(allJobs);
mockMvc.perform(get("/api/v1/test")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
感谢任何帮助、提示或建议!
非常感谢@pvpkiran!我还必须将 JobValidator 添加为 Mock!
@RunWith(SpringRunner.class)
@WebMvcTest(JobsController.class)
@AutoConfigureMockMvc
public class MailMonitorJobControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private JobService jobService;
@MockBean
private JobValidator jobValidator;
@Test
public void givenJobs_whenGetJobs_thenReturnJsonArray() throws Exception {
Job job = new Job("TEST");
List<Job> allJobs = Arrays.asList(job);
Mockito.when(jobService.getAllJobs()).thenReturn(allJobs);
mockMvc.perform(get("/api/v1/test")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}