java - Java test for an exception
问题描述
I have a class with this method
@Test
public void testWithOriginalPattern() throws Exception {
this.OBJCET_CONTENT =
"{\"service_instance\": \"foo\", \"notifications\": {\"subject\": \"bar0-9A-Za-z-._\"}}";
final HttpServletRequest request = createGoodRequest(this.OBJCET_CONTENT);
final ContainerMetadataManagementServlet containerMetadataManagementServlet =
new MockContainerMetadataManagementServlet();
assertNotNull(containerMetadataManagementServlet.runGetConfig(request, "/service-api-create-bucket-schema.json"));
}
I now want to do the test with invalid characters in the subject. When this happens the following exception is thrown:
HttpException(400,{"status_code":400,"error_code":"MalformedNotificationsError","uri":"uri","message":"The provided JSON was not well-formed or did not validate against the published schema."},null)
How do I test for that?
解决方案
修改您的测试方法,如下面的代码:
@Test(expected = StatusRuntimeException.class)
public void test01()
{
containerMetadataManagementServlet.runGetConfig(request,"/service-api-create-bucket-schema.json");
}
推荐阅读
- php - Enlist active 提交 0 而不是 1 和 1 而不是 0 但仍然给出正确的 Flash Success
- android - 如何在用户使用我的应用程序时禁用使用 NFC 打开浏览器?
- r - 在 Shiny 中使用 igraph 对象刷亮选项
- c++ - 我非常困惑,在 for 循环之外,一个值可能是一回事,但在里面却是另一回事?
- javascript - 如何在node.js中将数组作为参数传递给jar exec
- python - 如何在 Python 3 中从反斜杠符号中拆分反斜杠
- postgresql - 是否可以在 centos 上将 Postgresql 从版本 9.2 更新到 10
- html - 完全新手!非常迷失如何通过烧瓶发送 json 数据以显示在 HTML 网页上?
- javascript - 使用 ngx-datatable 在 Angular 中实现 ElasticSearch 滚动 ID?
- java - 如何在模型中包含列表?