diff --git a/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/InstrumentedInvokerFactoryTest.java b/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/InstrumentedInvokerFactoryTest.java index 36469d7..8d544d7 100644 --- a/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/InstrumentedInvokerFactoryTest.java +++ b/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/InstrumentedInvokerFactoryTest.java @@ -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; @@ -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); diff --git a/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/JAXWSBundleTest.java b/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/JAXWSBundleTest.java index c8d7906..15286e4 100644 --- a/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/JAXWSBundleTest.java +++ b/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/JAXWSBundleTest.java @@ -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; @@ -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 @@ -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); @@ -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); diff --git a/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/ValidatingInvokerTest.java b/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/ValidatingInvokerTest.java index 497914b..f33bf06 100644 --- a/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/ValidatingInvokerTest.java +++ b/dropwizard-jaxws/src/test/java/com/roskart/dropwizard/jaxws/ValidatingInvokerTest.java @@ -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; @@ -150,40 +150,21 @@ public void handleResponse(Response res) { @Test void invokeWithValidation() { - setTargetMethod(exchange, "withValidation", RootParam1.class, RootParam2.class); - List 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);