Using assertThatThrownBy (#4047)

This commit is contained in:
tamasradu 2018-04-20 00:28:38 +03:00 committed by maibin
parent a1b206ead8
commit ed9f3fa2e2

View File

@ -52,16 +52,12 @@ public class EmbeddedChannelUnitTest {
"/calculate?a=10&b=5"); "/calculate?a=10&b=5");
wrongHttpRequest.headers().add("Operator", "Add"); wrongHttpRequest.headers().add("Operator", "Add");
Throwable thrownException = catchThrowable(() -> { assertThatThrownBy(() -> {
// send invalid HTTP request to server and expect and error // send invalid HTTP request to server and expect and error
channel.pipeline().fireChannelRead(wrongHttpRequest); channel.pipeline().fireChannelRead(wrongHttpRequest);
channel.checkException(); channel.checkException();
Assertions.failBecauseExceptionWasNotThrown(UnsupportedOperationException.class); }).isInstanceOf(UnsupportedOperationException.class)
}); .hasMessage("HTTP method not supported");
assertThat(thrownException)
.isInstanceOf(UnsupportedOperationException.class)
.hasMessage("HTTP method not supported");
FullHttpResponse errorHttpResponse = channel.readOutbound(); FullHttpResponse errorHttpResponse = channel.readOutbound();
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset()); String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
@ -77,15 +73,11 @@ public class EmbeddedChannelUnitTest {
"/calculate?a=10&b=5"); "/calculate?a=10&b=5");
wrongHttpRequest.headers().add("Operator", "Invalid_operation"); wrongHttpRequest.headers().add("Operator", "Invalid_operation");
Throwable thrownException = catchThrowable(() -> { // the HttpMessageHandler does not handle the exception and throws it down the pipeline
// send invalid HTTP request to server and expect and error assertThatThrownBy(() -> {
channel.writeInbound(wrongHttpRequest); channel.writeInbound(wrongHttpRequest);
Assertions.failBecauseExceptionWasNotThrown(IllegalArgumentException.class); }).isInstanceOf(IllegalArgumentException.class)
}); .hasMessage("Operation not defined");
// the HttpMessageHandler does not handle the exception and throws it down the
// pipeline
assertThat(thrownException).isInstanceOf(IllegalArgumentException.class).hasMessage("Operation not defined");
// the outbound message is a HTTP response with the status code 500 // the outbound message is a HTTP response with the status code 500
FullHttpResponse errorHttpResponse = channel.readOutbound(); FullHttpResponse errorHttpResponse = channel.readOutbound();