Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up exception assertions #23

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading