Skip to content

Commit

Permalink
Clean up exception assertions
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
sleberknight committed Nov 6, 2023
1 parent c3aaa14 commit 5bc6727
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.lang.reflect.Method;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
Expand Down Expand Up @@ -218,15 +219,9 @@ void exceptionMeteredAnnotation() {

// Invoke InstrumentedResource.exceptionMetered with exception beeing thrown

invoker = invokerBuilder.create(instrumentedService, new ExceptionMeteredInvoker(true));
var throwingInvoker = invokerBuilder.create(instrumentedService, new ExceptionMeteredInvoker(true));

try {
invoker.invoke(exchange, null);
fail("Exception should be thrown here");
}
catch (Exception e) {
assertThat(e).isInstanceOf(RuntimeException.class);
}
assertThatRuntimeException().isThrownBy(() -> throwingInvoker.invoke(exchange, null));

assertThat(timer.getCount()).isEqualTo(oldtimervalue);
assertThat(meter.getCount()).isEqualTo(oldmetervalue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import jakarta.servlet.http.HttpServlet;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.startsWith;
Expand Down Expand Up @@ -44,21 +45,20 @@ void setUp() {

@Test
void constructorArgumentChecks() {
try {
new JAXWSBundle<>(null, null);
fail("expected IllegalArgumentException but no exception thrown");
}
catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
assertThatIllegalArgumentException()
.isThrownBy(() -> new JAXWSBundle<>(null, jaxwsEnvironment))
.withMessage("Servlet path is null");

try {
new JAXWSBundle<>("soap", null);
fail("expected IllegalArgumentException but no exception thrown");
}
catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
assertThatIllegalArgumentException()
.isThrownBy(() -> new JAXWSBundle<>("soap", jaxwsEnvironment))
.withMessage("soap is not an absolute path");

assertThatIllegalArgumentException()
.isThrownBy(() -> new JAXWSBundle<>("/soap", null))
.withMessage("jaxwsEnvironment is null");

assertThatCode(() -> new JAXWSBundle<>("/soap", jaxwsEnvironment))
.doesNotThrowAnyException();
}

@Test
Expand Down Expand Up @@ -113,29 +113,17 @@ void publishEndpoint() {

JAXWSBundle<?> jaxwsBundle = new JAXWSBundle<>("/soap", jaxwsEnvironment);
Object service = new Object();
try {
jaxwsBundle.publishEndpoint(new EndpointBuilder("foo", null));
fail("expected IllegalArgumentException but no exception thrown");
}
catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
assertThatIllegalArgumentException()
.isThrownBy(() -> jaxwsBundle.publishEndpoint(new EndpointBuilder("foo", null)))
.withMessage("Service is null");

try {
jaxwsBundle.publishEndpoint(new EndpointBuilder(null, service));
fail("expected IllegalArgumentException but no exception thrown");
}
catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
assertThatIllegalArgumentException()
.isThrownBy(() -> jaxwsBundle.publishEndpoint(new EndpointBuilder(null, service)))
.withMessage("Path is null");

try {
jaxwsBundle.publishEndpoint(new EndpointBuilder(" ", service));
fail("expected IllegalArgumentException but no exception thrown");
}
catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
assertThatIllegalArgumentException()
.isThrownBy(() -> jaxwsBundle.publishEndpoint(new EndpointBuilder(" ", service)))
.withMessage("Path is empty");

EndpointBuilder builder = mock(EndpointBuilder.class);
jaxwsBundle.publishEndpoint(builder);
Expand All @@ -150,30 +138,21 @@ void getClient() {
Class<?> cls = Object.class;
String url = "http://foo";

try {
jaxwsBundle.getClient(new ClientBuilder<>(null, null));
fail("expected IllegalArgumentException but no exception thrown");
}
assertThatIllegalArgumentException()
.isThrownBy(() -> jaxwsBundle.getClient(new ClientBuilder<>(null, null)))
.withMessage("ServiceClass is null");

catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
assertThatIllegalArgumentException()
.isThrownBy(() -> jaxwsBundle.getClient(new ClientBuilder<>(null, url)))
.withMessage("ServiceClass is null");

try {
jaxwsBundle.getClient(new ClientBuilder<>(null, url));
fail("expected IllegalArgumentException but no exception thrown");
}
catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
assertThatIllegalArgumentException()
.isThrownBy(() -> jaxwsBundle.getClient(new ClientBuilder<>(cls, null)))
.withMessage("Address is null");

try {
jaxwsBundle.getClient(new ClientBuilder<>(cls, " "));
fail("expected IllegalArgumentException but no exception thrown");
}
catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
assertThatIllegalArgumentException()
.isThrownBy(() -> jaxwsBundle.getClient(new ClientBuilder<>(cls, " ")))
.withMessage("Address is empty");

ClientBuilder<?> builder = new ClientBuilder<>(cls, url);
jaxwsBundle.getClient(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -150,40 +150,21 @@ public void handleResponse(Response res) {

@Test
void invokeWithValidation() {

setTargetMethod(exchange, "withValidation", RootParam1.class, RootParam2.class);

List<Object> params = Arrays.asList(new RootParam1(new ChildParam("")), new RootParam2("ok"));
assertThatThrownBy(() -> invoker.invoke(exchange, List.of(new RootParam1(new ChildParam("")), new RootParam2("ok"))))
.isExactlyInstanceOf(ValidationException.class);

try {
invoker.invoke(exchange, params);
fail("expected ValidationException but no exception thrown");
}
catch(Exception e) {
assertThat(e).isInstanceOf(ValidationException.class);
}
assertThatThrownBy(() -> invoker.invoke(exchange, List.of(new RootParam1(new ChildParam("ok")), new RootParam2(""))))
.isExactlyInstanceOf(ValidationException.class);

params = Arrays.asList(new RootParam1(new ChildParam("")), new RootParam2("ok"));
try {
invoker.invoke(exchange, params);
fail("expected ValidationException but no exception thrown");
}
catch(Exception e) {
assertThat(e).isInstanceOf(ValidationException.class);
}

params = Arrays.asList(new RootParam1(new ChildParam("John")), new RootParam2("ok"));
try {
invoker.invoke(exchange, params);
fail("expected ValidationException but no exception thrown");
}
catch(Exception e) {
assertThat(e).isInstanceOf(ValidationException.class);
}
assertThatThrownBy(() -> invoker.invoke(exchange, List.of(new RootParam1(new ChildParam("John")), new RootParam2("ok"))))
.isExactlyInstanceOf(ValidationException.class)
.hasMessageContaining("foo may not be 'John'");

verifyNoMoreInteractions(underlying);

params = Arrays.asList(new RootParam1(new ChildParam("ok")), new RootParam2("ok"));
var params = List.of(new RootParam1(new ChildParam("ok")), new RootParam2("ok"));
invoker.invoke(exchange, params);

verify(underlying).invoke(exchange, params);
Expand Down

0 comments on commit 5bc6727

Please sign in to comment.