0

I have a spring app that calls external APIs (I don't have control over this external project and its code). I am writing integration tests in my spring project. The test invoke those APIs. One of the API is throwing an exception for certain inputs:

On the tests, I am catching exception as:

Throwable throwable = catchThrowable(() -> TestHelper.createProduct(...));

assertThat(throwable)
  .isInstanceOf(CustomException.class)
  .hasMessageContaining(PRODUCT_CREATE_ERROR)
  .hasCause(null);

I tried printing throwable.getMessage() and this is what it prints:

An internal error occurred: status code: [400], status text: [Bad Request], 
headers: [[Transfer-Encoding:"chunked", Content-Type:"application/json; charset=utf-8", Server:"Kestrel", X-Powered-By:"ASP.NET", Date:"Fri, 10 Sep 2017 22:38:37 GMT"]], 
body: [{"status":400,"detail":"Cannot create product."}]

I want to be able to capture the message "Cannot create product", I see in the message above and use that to confirm that this error message was returned, something like:

throwable.
.hasMessageContaining(capturedMessage)

Is there a way to get this message "Cannot create product.", I see in the body detail above (from exception.getMessage)

If I do something like: .hasMessageContaining("An internal error occurred"), it works, but I also want to be able to do something similar for the err text "Cannot create product.". How can I retrieve that specific text and say that this error was returned?

user1892775
  • 2,001
  • 6
  • 37
  • 58

1 Answers1

0

Give a try to hasStackTraceContaining, that should do the job.

Joel Costigliola
  • 6,308
  • 27
  • 35