Micronaut:执行 Mockito 测试时不调用异常处理程序
Micronaut: Exception Handler is not invoked while executing Mockito tests
当我创建一个通过调用 API 代码抛出异常的情况时,当时 ExceptionHandler
被按预期调用。但是当我尝试通过单元测试创建相同的案例时,那时 ExceptionHandler
没有被调用。我的类如下:
Controller.java
@Post("/XXX")
public ResponseEntity<List<CategoryTopicBean>> listCategoryTopics(@Body CategoryIdsRequestBean categoryIdsRequestBean) {
if (categoryIdsRequestBean.getCategoryIds().size() > MAX_ALLOWED_CATEGORY_SELECTION) {
throw new CustomException(SystemConstants.ResponseCode.ERROR, SystemConstants.ResponseMessage.OVERFLOW_MAX_CATEGORIES);
}
...
CustomExceptionHandler.java:
@Produces
@Singleton
@Requires(classes = {CustomException.class, ExceptionHandler.class})
public class CustomExceptionHandler implements ExceptionHandler<CustomException, HttpResponse> {
@Override
public HttpResponse handle(HttpRequest request, CustomException exception) {
return HttpResponse.ok(new ResponseEntity<>(exception.responseCode, exception.getMessage()));
}
}
XXXShould.java
@Test
public void should_list_category_topics() {
CategoryIdsRequestBean categoryIdsBean = new CategoryIdsRequestBean();
IdBean idBean = new IdBean();
idBean.setId(ID_1);
categoryIdsBean.setCategoryIds(Arrays.asList(idBean));
ResponseEntity<List<CategoryTopicBean>> responseEntity = topicController.listCategoryTopics(categoryIdsBean);
assertThat(SystemConstants.ResponseCode.SUCCESS).isEqualTo(responseEntity.getResponseCode());
assertThat(1).isEqualTo(responseEntity.getData().size());
categoryIdsBean = new CategoryIdsRequestBean();
categoryIdsBean.setCategoryIds(Arrays.asList(idBean, idBean, idBean, idBean, idBean, idBean));
responseEntity = topicController.listCategoryTopics(categoryIdsBean);
}
任何人都可以调查一下并帮助我吗?
这里的问题是,您是通过直接调用控制器方法来测试控制器
topicController.listCategoryTopics(categoryIdsBean)
.
这不是测试控制器功能的好方法。你应该做的是使用 MicronautTest。 MicronautTest 将启动一个嵌入式服务器。现在您可以使用 HTTP 客户端访问端点并检索结果。
您的代码需要更改为以下几行。
@MicronautTest
class HelloWorldTest {
@Inject
@Client("/")
RxHttpClient client;
@Test
public void should_list_category_topics() {
// Test Data
CategoryIdsRequestBean categoryIdsBean = new CategoryIdsRequestBean();
IdBean idBean = new IdBean();
idBean.setId(ID_1);
categoryIdsBean.setCategoryIds(Arrays.asList(idBean));
HttpRequest<String> request = HttpRequest.POST("/XXX", categoryIdsBean);
ResponseEntity<List<CategoryTopicBean>> retrieve = client.toBlocking().retrieve(request, ResponseEntity.class);
categoryIdsBean = new CategoryIdsRequestBean();
categoryIdsBean.setCategoryIds(Arrays.asList(idBean, idBean, idBean, idBean, idBean, idBean));
responseEntity = topicController.listCategoryTopics(categoryIdsBean);
}
}
对于exception
案例场景,作为异常处理程序returnsResponseEntity<String>
,您需要对上述代码做一些小改动。
ResponseEntity<String> retrieve = client.toBlocking()
.retrieve(request, ResponseEntity.class);
如果您的控制器调用任何其他服务,请不要忘记模拟该行为。
当我创建一个通过调用 API 代码抛出异常的情况时,当时 ExceptionHandler
被按预期调用。但是当我尝试通过单元测试创建相同的案例时,那时 ExceptionHandler
没有被调用。我的类如下:
Controller.java
@Post("/XXX")
public ResponseEntity<List<CategoryTopicBean>> listCategoryTopics(@Body CategoryIdsRequestBean categoryIdsRequestBean) {
if (categoryIdsRequestBean.getCategoryIds().size() > MAX_ALLOWED_CATEGORY_SELECTION) {
throw new CustomException(SystemConstants.ResponseCode.ERROR, SystemConstants.ResponseMessage.OVERFLOW_MAX_CATEGORIES);
}
...
CustomExceptionHandler.java:
@Produces
@Singleton
@Requires(classes = {CustomException.class, ExceptionHandler.class})
public class CustomExceptionHandler implements ExceptionHandler<CustomException, HttpResponse> {
@Override
public HttpResponse handle(HttpRequest request, CustomException exception) {
return HttpResponse.ok(new ResponseEntity<>(exception.responseCode, exception.getMessage()));
}
}
XXXShould.java
@Test
public void should_list_category_topics() {
CategoryIdsRequestBean categoryIdsBean = new CategoryIdsRequestBean();
IdBean idBean = new IdBean();
idBean.setId(ID_1);
categoryIdsBean.setCategoryIds(Arrays.asList(idBean));
ResponseEntity<List<CategoryTopicBean>> responseEntity = topicController.listCategoryTopics(categoryIdsBean);
assertThat(SystemConstants.ResponseCode.SUCCESS).isEqualTo(responseEntity.getResponseCode());
assertThat(1).isEqualTo(responseEntity.getData().size());
categoryIdsBean = new CategoryIdsRequestBean();
categoryIdsBean.setCategoryIds(Arrays.asList(idBean, idBean, idBean, idBean, idBean, idBean));
responseEntity = topicController.listCategoryTopics(categoryIdsBean);
}
任何人都可以调查一下并帮助我吗?
这里的问题是,您是通过直接调用控制器方法来测试控制器
topicController.listCategoryTopics(categoryIdsBean)
.
这不是测试控制器功能的好方法。你应该做的是使用 MicronautTest。 MicronautTest 将启动一个嵌入式服务器。现在您可以使用 HTTP 客户端访问端点并检索结果。
您的代码需要更改为以下几行。
@MicronautTest
class HelloWorldTest {
@Inject
@Client("/")
RxHttpClient client;
@Test
public void should_list_category_topics() {
// Test Data
CategoryIdsRequestBean categoryIdsBean = new CategoryIdsRequestBean();
IdBean idBean = new IdBean();
idBean.setId(ID_1);
categoryIdsBean.setCategoryIds(Arrays.asList(idBean));
HttpRequest<String> request = HttpRequest.POST("/XXX", categoryIdsBean);
ResponseEntity<List<CategoryTopicBean>> retrieve = client.toBlocking().retrieve(request, ResponseEntity.class);
categoryIdsBean = new CategoryIdsRequestBean();
categoryIdsBean.setCategoryIds(Arrays.asList(idBean, idBean, idBean, idBean, idBean, idBean));
responseEntity = topicController.listCategoryTopics(categoryIdsBean);
}
}
对于exception
案例场景,作为异常处理程序returnsResponseEntity<String>
,您需要对上述代码做一些小改动。
ResponseEntity<String> retrieve = client.toBlocking()
.retrieve(request, ResponseEntity.class);
如果您的控制器调用任何其他服务,请不要忘记模拟该行为。