如何使用 JUnit 和 Mockito 通过静态 util 调用测试 Rest Controller
How to test Rest Controller with static util invocation using JUnit and Mockito
我有 Rest Controller 方法 create(validation using util class + databaseService(databaseDao + 缓存))
@RestController
@RequestMapping("files")
public class FilesController {
private IDbFilesDao dbFilesService;
private Map<String, Table> tables;
public FilesController(IDbFilesDao dbFilesService, Map<String, Table> tables) {
this.dbFilesService = dbFilesService;
this.tables = tables;
}
@PostMapping("{table}")
public ResponseEntity createTable(@PathVariable("table") String tableName,
@RequestBody File file) {
FilesValidator.validateAdding(tableName, tables, file);
dbFilesService.create(tableName, file);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand(file.getKey()).toUri();
return ResponseEntity.created(location).build();
}
}
我有一个测试:
@RunWith(SpringRunner.class)
@WebMvcTest(value = FilesController.class, secure = false)
public class FilesControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private IDbFilesDao dbFilesService;
@MockBean
private Map<String, Table> tables;
@Test
public void create() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/files/tableName")
.accept(MediaType.APPLICATION_JSON)
.content(POST_JSON_BODY)
.contentType(MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
assertEquals(HttpStatus.CREATED.value(), response.getStatus());
}
}
只有在@RestContoller 中没有这一行才能正常工作:
FilesValidator.validateAdding(tableName, tables, file);
此行 - 未找到 404。
FilesValidator
- 使用静态方法的 util class。它检查数据是否有效并且不执行任何操作或抛出带有状态代码(例如 404)的运行时异常。
如何在不删除的情况下修复它验证?
1) 将验证器调用移至包级方法并进行小规模重构:
@PostMapping("{table}")
public ResponseEntity createTable(@PathVariable("table") String tableName,
@RequestBody File file) {
validateAdding(tableName, tables, file);
...
}
validateAdding(String tableName, Map<String, Table> tables, File file){
FilesValidator.validateAdding(tableName, tables, file);
}
2) 在测试中侦测控制器:
@SpyBean
private FilesController filesControllerSpy;
3) 使 validateAdding
方法什么也不做:
@Test
public void create() throws Exception {
doNothing().when(filesControllerSpy)
.validateAdding(any(String.class), any(Map.class), any(File.class));
...
我有 Rest Controller 方法 create(validation using util class + databaseService(databaseDao + 缓存))
@RestController
@RequestMapping("files")
public class FilesController {
private IDbFilesDao dbFilesService;
private Map<String, Table> tables;
public FilesController(IDbFilesDao dbFilesService, Map<String, Table> tables) {
this.dbFilesService = dbFilesService;
this.tables = tables;
}
@PostMapping("{table}")
public ResponseEntity createTable(@PathVariable("table") String tableName,
@RequestBody File file) {
FilesValidator.validateAdding(tableName, tables, file);
dbFilesService.create(tableName, file);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand(file.getKey()).toUri();
return ResponseEntity.created(location).build();
}
}
我有一个测试:
@RunWith(SpringRunner.class)
@WebMvcTest(value = FilesController.class, secure = false)
public class FilesControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private IDbFilesDao dbFilesService;
@MockBean
private Map<String, Table> tables;
@Test
public void create() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/files/tableName")
.accept(MediaType.APPLICATION_JSON)
.content(POST_JSON_BODY)
.contentType(MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
assertEquals(HttpStatus.CREATED.value(), response.getStatus());
}
}
只有在@RestContoller 中没有这一行才能正常工作:
FilesValidator.validateAdding(tableName, tables, file);
此行 - 未找到 404。
FilesValidator
- 使用静态方法的 util class。它检查数据是否有效并且不执行任何操作或抛出带有状态代码(例如 404)的运行时异常。
如何在不删除的情况下修复它验证?
1) 将验证器调用移至包级方法并进行小规模重构:
@PostMapping("{table}")
public ResponseEntity createTable(@PathVariable("table") String tableName,
@RequestBody File file) {
validateAdding(tableName, tables, file);
...
}
validateAdding(String tableName, Map<String, Table> tables, File file){
FilesValidator.validateAdding(tableName, tables, file);
}
2) 在测试中侦测控制器:
@SpyBean
private FilesController filesControllerSpy;
3) 使 validateAdding
方法什么也不做:
@Test
public void create() throws Exception {
doNothing().when(filesControllerSpy)
.validateAdding(any(String.class), any(Map.class), any(File.class));
...