Skip to content
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 @@ -36,9 +36,7 @@
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
Expand All @@ -50,6 +48,7 @@
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

/**
Expand All @@ -59,9 +58,6 @@
*/
public class CxfAutoConfigurationTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

private AnnotationConfigWebApplicationContext context;

@After
Expand All @@ -80,13 +76,13 @@ public void defaultConfiguration() {

@Test
public void customPathMustBeginWithASlash() {
this.thrown.expect(UnsatisfiedDependencyException.class);
this.thrown.expectCause(
UnsatisfiedDependencyException exception = assertThrows(UnsatisfiedDependencyException.class,
() -> load(CxfAutoConfiguration.class, "cxf.path=invalid"));
assertThat(exception.getCause(),
allOf(instanceOf(ConfigurationPropertiesBindException.class), hasProperty("cause",
allOf(instanceOf(BindException.class), hasProperty("cause",
allOf(instanceOf(BindValidationException.class),
hasProperty("message", containsString("Path must start with /"))))))));
load(CxfAutoConfiguration.class, "cxf.path=invalid");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,14 @@
import org.apache.cxf.jaxrs.resources.UriBuilderWrongAnnotations;
import org.apache.cxf.jaxrs.utils.JAXRSUtils;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

public class UriBuilderImplTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testFromUriRelativePath() throws Exception {
UriBuilder builder = UriBuilder.fromUri("path");
Expand Down Expand Up @@ -863,12 +858,11 @@ public void testAddPathMethodNull() throws Exception {

@Test
public void testAddPathMethodNoAnnotation() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
String.format("Method '%s.getBook' is not annotated with Path",
BookStore.class.getCanonicalName()));
Method noAnnot = BookStore.class.getMethod("getBook", String.class);
new UriBuilderImpl().path(noAnnot).build();
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> new UriBuilderImpl().path(noAnnot).build());
assertEquals(String.format("Method '%s.getBook' is not annotated with Path",
BookStore.class.getCanonicalName()), ex.getMessage());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import java.net.SocketException;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class ThreadLocalInvocationHandlerTest {

Expand All @@ -37,13 +38,6 @@ public class ThreadLocalInvocationHandlerTest {

private TestIface testIface;

private ExpectedException expectedException = ExpectedException.none();

@Rule
public ExpectedException getExpectedExceptionRule() {
return expectedException;
}

@Before
public void setUp() throws Exception {
ThreadLocalInvocationHandler<TestClass> subject = new ThreadLocalInvocationHandler<>();
Expand All @@ -55,34 +49,30 @@ public void setUp() throws Exception {

@Test
public void testCheckedExceptionPropagation() throws Exception {
expectedException.expect(SocketException.class);
expectedException.expectMessage(CHECKED_EXCEPTION_MSG);

testIface.throwCheckedException();
SocketException ex = assertThrows(SocketException.class,
() -> testIface.throwCheckedException());
assertEquals(CHECKED_EXCEPTION_MSG, ex.getMessage());
}

@Test
public void testUncheckedExceptionPropagation() {
expectedException.expect(IndexOutOfBoundsException.class);
expectedException.expectMessage(UNCHECKED_EXCEPTION_MSG);

testIface.throwUncheckedException();
IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class,
() -> testIface.throwUncheckedException());
assertEquals(UNCHECKED_EXCEPTION_MSG, ex.getMessage());
}

@Test
public void testErrorPropagation() {
expectedException.expect(AnnotationFormatError.class);
expectedException.expectMessage(ERROR_MSG);

testIface.throwError();
AnnotationFormatError ex = assertThrows(AnnotationFormatError.class,
() -> testIface.throwError());
assertEquals(ERROR_MSG, ex.getMessage());
}

@Test
public void testThrowablePropagation() throws Throwable {
expectedException.expect(Throwable.class);
expectedException.expectMessage(THROWABLE_MSG);

testIface.throwThrowable();
Throwable ex = assertThrows(Throwable.class,
() -> testIface.throwThrowable());
assertEquals(THROWABLE_MSG, ex.getMessage());
}

private interface TestIface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,17 @@
import org.apache.cxf.transport.http.AbstractHTTPDestination;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class SseEventSinkContextProviderTest {
private static final OutboundSseEvent EVENT = new OutboundSseEventImpl.BuilderImpl().build();

@Rule
public ExpectedException exception = ExpectedException.none();

private SseEventSinkContextProvider provider;
private Message message;
Expand Down Expand Up @@ -96,11 +93,10 @@ public void testCreateSseEventSinkWithDefaultBufferSize() {
// The buffer overflow should trigger message rejection and exceptional completion
final CompletableFuture<?> overflow = sink.send(EVENT).toCompletableFuture();
assertThat(overflow.isCompletedExceptionally(), equalTo(true));

exception.expect(CompletionException.class);
exception.expectMessage("The buffer is full (10000), unable to queue SSE event for send.");

overflow.join();
final CompletionException ex = assertThrows(CompletionException.class, overflow::join);
assertThat(ex.getCause().getMessage(),
startsWith("The buffer is full (10000)"));
}

@Test
Expand All @@ -118,10 +114,9 @@ public void testCreateSseEventSinkWithCustomBufferSize() {
// The buffer overflow should trigger message rejection and exceptional completion
final CompletableFuture<?> overflow = sink.send(EVENT).toCompletableFuture();
assertThat(overflow.isCompletedExceptionally(), equalTo(true));

exception.expect(CompletionException.class);
exception.expectMessage("The buffer is full (20000), unable to queue SSE event for send.");

overflow.join();
final CompletionException ex = assertThrows(CompletionException.class, overflow::join);
assertThat(ex.getCause().getMessage(),
startsWith("The buffer is full (20000)"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;


public class SseEventSourceImplTest {
Expand Down Expand Up @@ -109,9 +108,6 @@ enum Type {

private static final Map<Type, Server> SERVERS = new EnumMap<>(Type.class);

@Rule
public ExpectedException exception = ExpectedException.none();

private final List<InboundSseEvent> events = new ArrayList<>();
private final List<Throwable> errors = new ArrayList<>();

Expand Down Expand Up @@ -207,8 +203,8 @@ public void testWhenTryToConnectTwiceSecondAttemtShouldFail() throws Interrupted
// The attempt to open the SSE connection in another thread at the same
// time should fail
final Future<?> future = executor.submit(() -> eventSource.open());
exception.expectCause(instanceOf(IllegalStateException.class));
assertThat(future.get(), equalTo(null));
final ExecutionException ex = assertThrows(ExecutionException.class, future::get);
assertThat(ex.getCause(), instanceOf(IllegalStateException.class));

assertThat(eventSource.isOpen(), equalTo(true));
assertThat(events.size(), equalTo(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
Expand All @@ -53,14 +52,14 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.times;

@RunWith(MockitoJUnitRunner.class)
public class JAXRSClientMetricsTest {
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
@Rule public ExpectedException expectedException = ExpectedException.none();

private MetricsProvider provider;
private MetricsContext operationContext;
Expand Down Expand Up @@ -102,8 +101,7 @@ public void usingClientProxyStopIsCalledWhenServerReturnsNotFound() throws Excep

try {
final Library client = factory.create(Library.class);
expectedException.expect(NotFoundException.class);
client.getBook(10);
assertThrows(NotFoundException.class, () -> client.getBook(10));
} finally {
Mockito.verify(resourceContext, times(1)).start(any(Exchange.class));
Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
Expand All @@ -123,11 +121,11 @@ public void usingClientStopIsCalledWhenServerReturnsNotFound() throws Exception
.withStatus(404)));

try {
expectedException.expect(ProcessingException.class);
client
.target("http://localhost:" + wireMockRule.port() + "/books/10")
.request(MediaType.APPLICATION_JSON).get()
.readEntity(Book.class);
assertThrows(ProcessingException.class,
() -> client
.target("http://localhost:" + wireMockRule.port() + "/books/10")
.request(MediaType.APPLICATION_JSON).get()
.readEntity(Book.class));
} finally {
Mockito.verify(resourceContext, times(1)).start(any(Exchange.class));
Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
Expand All @@ -145,12 +143,12 @@ public void usingClientStopIsCalledWhenConnectionIsRefused() throws Exception {
.register(JacksonJsonProvider.class);

try {
expectedException.expect(ProcessingException.class);
client
.target("http://localhost:" + port + "/books/10")
.request(MediaType.APPLICATION_JSON)
.get()
.readEntity(Book.class);
assertThrows(ProcessingException.class,
() -> client
.target("http://localhost:" + port + "/books/10")
.request(MediaType.APPLICATION_JSON)
.get()
.readEntity(Book.class));
} finally {
Mockito.verify(resourceContext, times(1)).start(any(Exchange.class));
Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
Expand Down Expand Up @@ -195,8 +193,7 @@ public void usingWebClientStopIsCalledWhenServerReturnsNotFound() throws Excepti
.withStatus(404)));

try {
expectedException.expect(ProcessingException.class);
client.get().readEntity(Book.class);
assertThrows(ProcessingException.class, () -> client.get().readEntity(Book.class));
} finally {
Mockito.verify(resourceContext, times(1)).start(any(Exchange.class));
Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
Expand All @@ -63,8 +62,6 @@ public class JAXRSServerMetricsTest extends AbstractClientServerTestBase {
private static MetricsProvider provider;
private static MetricsContext operationContext;
private static MetricsContext resourceContext;

@Rule public ExpectedException expectedException = ExpectedException.none();

public static class BookLibrary implements Library {
@Override
Expand Down Expand Up @@ -135,8 +132,7 @@ public void usingClientProxyStopIsCalledWhenServerReturnsNotFound() throws Excep

try {
final Library client = factory.create(Library.class);
expectedException.expect(NotFoundException.class);
client.getBook(10);
assertThrows(NotFoundException.class, () -> client.getBook(10));
} finally {
Mockito.verify(resourceContext, times(1)).start(any(Exchange.class));
Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
Expand All @@ -151,11 +147,11 @@ public void usingClientStopIsCalledWhenServerReturnsNotFound() throws Exception
.register(JacksonJsonProvider.class);

try {
expectedException.expect(ProcessingException.class);
client
.target("http://localhost:" + PORT + "/books/10")
.request(MediaType.APPLICATION_JSON).get()
.readEntity(Book.class);
assertThrows(ProcessingException.class,
() -> client
.target("http://localhost:" + PORT + "/books/10")
.request(MediaType.APPLICATION_JSON).get()
.readEntity(Book.class));
} finally {
Mockito.verify(resourceContext, times(1)).start(any(Exchange.class));
Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
Expand Down Expand Up @@ -188,8 +184,7 @@ public void usingWebClientStopIsCalledWhenServerReturnsNotFound() throws Excepti
Arrays.asList(JacksonJsonProvider.class));

try {
expectedException.expect(ProcessingException.class);
client.get().readEntity(Book.class);
assertThrows(ProcessingException.class, () -> client.get().readEntity(Book.class));
} finally {
Mockito.verify(resourceContext, times(1)).start(any(Exchange.class));
Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
Expand All @@ -203,8 +198,7 @@ public void usingWebClientStopIsCalledWhenUrlIsNotServed() throws Exception {
Arrays.asList(JacksonJsonProvider.class));

try {
expectedException.expect(ProcessingException.class);
client.get().readEntity(Book.class);
assertThrows(ProcessingException.class, () -> client.get().readEntity(Book.class));
} finally {
Mockito.verifyNoInteractions(resourceContext);
Mockito.verifyNoInteractions(operationContext);
Expand Down
Loading