首页 > 解决方案 > 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?

标签: javaunit-testingtestingexceptionjunit

解决方案


修改您的测试方法,如下面的代码:

@Test(expected = StatusRuntimeException.class)
public void test01() 
{
  containerMetadataManagementServlet.runGetConfig(request,"/service-api-create-bucket-schema.json");
}

推荐阅读