Skip to content

Commit

Permalink
Migrate servicetalk-concurrent-reactivestreams tests to Junit 5 (#1628)
Browse files Browse the repository at this point in the history
Motivation:
JUnit 5 leverages features from Java 8 or later, such as lambda functions, making tests more powerful and easier to maintain.
JUnit 5 has added some very useful new features for describing, organizing, and executing tests. For instance, tests get better display names and can be organized hierarchically.
JUnit 5 is organized into multiple libraries, so only the features you need are imported into your project. With build systems such as Maven and Gradle, including the right libraries is easy.
JUnit 5 can use more than one extension at a time, which JUnit 4 could not (only one runner could be used at a time). This means you can easily combine the Spring extension with other extensions (such as your own custom extension).

Modifications:
Unit tests have been migrated from JUnit 4 to JUnit 5

Result:
Module servicetalk-concurrent-reactivestreams now runs tests using JUnit 5
  • Loading branch information
danfaer authored and bondolo committed Jun 28, 2021
1 parent ebfedfa commit 60d7896
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
4 changes: 3 additions & 1 deletion servicetalk-concurrent-reactivestreams/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ dependencies {
testImplementation testFixtures(project(":servicetalk-concurrent-api"))
testImplementation testFixtures(project(":servicetalk-concurrent-internal"))
testImplementation project(":servicetalk-test-resources")
testImplementation "junit:junit:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"
testImplementation "org.hamcrest:hamcrest-library:$hamcrestVersion"
testImplementation "org.reactivestreams:reactive-streams-tck:$reactiveStreamsVersion"
testImplementation "org.testng:testng:$testngVersion"

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5Version"
}

// We need testng for the reactive-streams-tck
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import io.servicetalk.concurrent.api.TestSubscription;
import io.servicetalk.concurrent.internal.ScalarValueSubscription;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
Expand All @@ -49,24 +47,21 @@
import static io.servicetalk.concurrent.reactivestreams.ReactiveStreamsAdapters.fromReactiveStreamsPublisher;
import static io.servicetalk.concurrent.reactivestreams.ReactiveStreamsAdapters.toReactiveStreamsPublisher;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

public class ReactiveStreamsAdaptersTest {

@Rule
public final ExpectedException expectedException = ExpectedException.none();
class ReactiveStreamsAdaptersTest {

@Test
public void fromRSSuccess() throws Exception {
void fromRSSuccess() throws Exception {
Publisher<Integer> rsPublisher = newMockRsPublisher((subscriber, __) -> {
subscriber.onNext(1);
subscriber.onComplete();
Expand All @@ -76,17 +71,16 @@ public void fromRSSuccess() throws Exception {
}

@Test
public void fromRSError() throws Exception {
void fromRSError() {
Publisher<Integer> rsPublisher = newMockRsPublisher((subscriber, __) ->
subscriber.onError(DELIBERATE_EXCEPTION));
Future<Integer> future = fromReactiveStreamsPublisher(rsPublisher).firstOrElse(() -> null).toFuture();
expectedException.expect(instanceOf(ExecutionException.class));
expectedException.expectCause(sameInstance(DELIBERATE_EXCEPTION));
future.get();
ExecutionException ex = assertThrows(ExecutionException.class, future::get);
assertThat(ex.getCause(), sameInstance(DELIBERATE_EXCEPTION));
}

@Test
public void fromRSCancel() {
void fromRSCancel() {
AtomicReference<Subscription> receivedSubscription = new AtomicReference<>();
Publisher<Integer> rsPublisher = newMockRsPublisher((__, subscription) ->
receivedSubscription.set(subscription));
Expand All @@ -97,12 +91,12 @@ public void fromRSCancel() {
}

@Test
public void singleToRSSuccess() {
void singleToRSSuccess() {
verifyRSSuccess(toRSPublisherAndSubscribe(succeeded(1)), true);
}

@Test
public void singleToRSFromSourceSuccess() {
void singleToRSFromSourceSuccess() {
SingleSource<Integer> source = s -> {
s.onSubscribe(IGNORE_CANCEL);
s.onSuccess(1);
Expand All @@ -111,12 +105,12 @@ public void singleToRSFromSourceSuccess() {
}

@Test
public void completableToRSSuccess() {
void completableToRSSuccess() {
verifyRSSuccess(toRSPublisherAndSubscribe(Completable.completed()), false);
}

@Test
public void completableToRSFromSourceSuccess() {
void completableToRSFromSourceSuccess() {
CompletableSource source = s -> {
s.onSubscribe(IGNORE_CANCEL);
s.onComplete();
Expand All @@ -125,12 +119,12 @@ public void completableToRSFromSourceSuccess() {
}

@Test
public void toRSSuccess() {
void toRSSuccess() {
verifyRSSuccess(toRSPublisherAndSubscribe(from(1)), true);
}

@Test
public void toRSFromSourceSuccess() {
void toRSFromSourceSuccess() {
PublisherSource<Integer> source = s -> s.onSubscribe(new ScalarValueSubscription<>(1, s));
verifyRSSuccess(toRSPublisherFromSourceAndSubscribe(source), true);
}
Expand All @@ -145,22 +139,22 @@ private void verifyRSSuccess(final Subscriber<Integer> subscriber, boolean onNex
}

@Test
public void singleToRSError() {
void singleToRSError() {
verifyRSError(toRSPublisherAndSubscribe(Single.failed(DELIBERATE_EXCEPTION)));
}

@Test
public void completableToRSError() {
void completableToRSError() {
verifyRSError(toRSPublisherAndSubscribe(Completable.failed(DELIBERATE_EXCEPTION)));
}

@Test
public void toRSError() {
void toRSError() {
verifyRSError(toRSPublisherAndSubscribe(failed(DELIBERATE_EXCEPTION)));
}

@Test
public void singleToRSFromSourceError() {
void singleToRSFromSourceError() {
SingleSource<Integer> source = s -> {
s.onSubscribe(IGNORE_CANCEL);
s.onError(DELIBERATE_EXCEPTION);
Expand All @@ -169,7 +163,7 @@ public void singleToRSFromSourceError() {
}

@Test
public void completableToRSFromSourceError() {
void completableToRSFromSourceError() {
CompletableSource source = s -> {
s.onSubscribe(EMPTY_SUBSCRIPTION);
s.onError(DELIBERATE_EXCEPTION);
Expand All @@ -178,7 +172,7 @@ public void completableToRSFromSourceError() {
}

@Test
public void toRSFromSourceError() {
void toRSFromSourceError() {
PublisherSource<Integer> source = s -> {
s.onSubscribe(EMPTY_SUBSCRIPTION);
s.onError(DELIBERATE_EXCEPTION);
Expand All @@ -193,7 +187,7 @@ private void verifyRSError(final Subscriber<Integer> subscriber) {
}

@Test
public void singleToRSCancel() {
void singleToRSCancel() {
TestSingle<Integer> stSingle = new TestSingle<>();
Subscriber<Integer> subscriber = toRSPublisherAndSubscribe(stSingle);
TestSubscription subscription = new TestSubscription();
Expand All @@ -206,7 +200,7 @@ public void singleToRSCancel() {
}

@Test
public void completableToRSCancel() {
void completableToRSCancel() {
TestCompletable stCompletable = new TestCompletable();
Subscriber<Integer> subscriber = toRSPublisherAndSubscribe(stCompletable);
TestSubscription subscription = new TestSubscription();
Expand All @@ -219,7 +213,7 @@ public void completableToRSCancel() {
}

@Test
public void toRSCancel() {
void toRSCancel() {
TestPublisher<Integer> stPublisher = new TestPublisher<>();
Subscriber<Integer> subscriber = toRSPublisherAndSubscribe(stPublisher);
TestSubscription subscription = new TestSubscription();
Expand All @@ -232,7 +226,7 @@ public void toRSCancel() {
}

@Test
public void singleToRSFromSourceCancel() {
void singleToRSFromSourceCancel() {
Cancellable srcCancellable = mock(Cancellable.class);
SingleSource<Integer> source = s -> s.onSubscribe(srcCancellable);
Subscriber<Integer> subscriber = toRSPublisherFromSourceAndSubscribe(source);
Expand All @@ -243,7 +237,7 @@ public void singleToRSFromSourceCancel() {
}

@Test
public void completableToRSFromSourceCancel() {
void completableToRSFromSourceCancel() {
Cancellable srcCancellable = mock(Cancellable.class);
CompletableSource source = s -> s.onSubscribe(srcCancellable);
Subscriber<Integer> subscriber = toRSPublisherFromSourceAndSubscribe(source);
Expand All @@ -254,7 +248,7 @@ public void completableToRSFromSourceCancel() {
}

@Test
public void toRSFromSourceCancel() {
void toRSFromSourceCancel() {
PublisherSource.Subscription srcSubscription = mock(PublisherSource.Subscription.class);
PublisherSource<Integer> source = s -> s.onSubscribe(srcSubscription);
Subscriber<Integer> subscriber = toRSPublisherFromSourceAndSubscribe(source);
Expand Down

0 comments on commit 60d7896

Please sign in to comment.