异常的Java测试

异常的Java测试,java,unit-testing,testing,exception,junit,Java,Unit Testing,Testing,Exception,Junit,我有一个用这种方法的班级 @测试 public void testWithOriginalPattern()引发异常{ 此.OBJCET_内容= “{\'service\'u instance\':\'foo\',\'notifications\':{\'subject\':\'bar0-9A-Za-z-.\u\'}”; 最终HttpServletRequest请求=createGoodRequest(this.OBJCET_内容); 最终容器元数据管理servlet容器元数据管理servlet

我有一个用这种方法的班级

@测试
public void testWithOriginalPattern()引发异常{
此.OBJCET_内容=
“{\'service\'u instance\':\'foo\',\'notifications\':{\'subject\':\'bar0-9A-Za-z-.\u\'}”;
最终HttpServletRequest请求=createGoodRequest(this.OBJCET_内容);
最终容器元数据管理servlet容器元数据管理servlet=
新的MockContainerMetadataManagementServlet();
assertNotNull(containerMetadataManagementServlet.runGetConfig(请求,“/service api create bucket schema.json”);
}
我现在想用主题中的无效字符进行测试。发生这种情况时,将引发以下异常:

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)

如何进行测试?

Junit5允许您使用Java8 Lambda语法进行assertThrows:

他们的例子是:

@Test
void testExpectedException() {

  Assertions.assertThrows(NumberFormatException.class, () -> {
    Integer.parseInt("One");
  });

}

我通过
@Test(expected=ServiceException.class)

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

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

这回答了你的问题吗?