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
  • 4,276
  • 6
  • 33
  • 80
  • 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
  • 7,532
  • 6
  • 42
  • 79
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 Golmahalle
  • 2,911
  • 2
  • 20
  • 31
Shifi
  • 26
  • 4
0

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

runnerpaul
  • 4,276
  • 6
  • 33
  • 80