1

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?

runnerpaul
  • 5,942
  • 8
  • 49
  • 118
  • 2
    Does this answer your question? [How do you assert that a certain exception is thrown in JUnit 4 tests?](https://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests) – Marged Jan 26 '20 at 16:01

3 Answers3

1

Junit5 allows you to assertThrows using Java8 Lambda syntax: https://howtodoinjava.com/junit5/expected-exception-example

Their example:

@Test
void testExpectedException() {

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

}
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
1

Modify your test method like below code:

@Test(expected = StatusRuntimeException.class)
public void test01() 
{
  containerMetadataManagementServlet.runGetConfig(request,"/service-api-create-bucket-schema.json");
}
Amin Golmahalleh
  • 3,585
  • 2
  • 23
  • 36
Shifi
  • 26
  • 4
0

I resolved it with @Test(expected = ServiceException.class)

runnerpaul
  • 5,942
  • 8
  • 49
  • 118