Java测试异常

标签 java unit-testing testing exception junit

我有一个使用此方法的类

@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"));
}

我现在想用主题中的无效字符进行测试。发生这种情况时,会引发以下异常:

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)

我该如何测试?

最佳答案

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

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

关于Java测试异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59920037/

相关文章:

java - 过载 : Ambigutiy while the parameters are primitive but not while it is Reference type

java - 使用 Knime 定义 Oozie 工作流程

string - 有单元测试的示例字符串吗? (例如使用控制字符)

iPhone内存测试

testing - 从使用testcafe开源升级到studio有用吗?

java - 无法@Autowire配置

java - 在客户端使用 GWT 构建 RSS

ruby - 如何强制队列同步运行以进行测试?

java - 如何在反射的私有(private)方法上模拟自动连线的私有(private)响应

java - 如何为 BigDecimal 编写 Junit 测试用例