Skip to content

Commit 45c0b06

Browse files
authored
3.x: Fix Junit 4.13 deprecated API use (#6813)
1 parent a864594 commit 45c0b06

File tree

4 files changed

+377
-411
lines changed

4 files changed

+377
-411
lines changed

src/test/java/io/reactivex/rxjava3/observers/TestObserverTest.java

Lines changed: 93 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
import java.util.*;
2121
import java.util.concurrent.TimeUnit;
2222

23-
import org.junit.*;
24-
import org.junit.rules.ExpectedException;
23+
import org.junit.Test;
2524
import org.mockito.InOrder;
2625
import org.reactivestreams.Subscriber;
2726

2827
import io.reactivex.rxjava3.core.*;
2928
import io.reactivex.rxjava3.core.Observable;
3029
import io.reactivex.rxjava3.core.Observer;
31-
import io.reactivex.rxjava3.disposables.*;
30+
import io.reactivex.rxjava3.disposables.Disposable;
3231
import io.reactivex.rxjava3.exceptions.TestException;
3332
import io.reactivex.rxjava3.functions.Predicate;
3433
import io.reactivex.rxjava3.internal.functions.Functions;
@@ -40,9 +39,6 @@
4039

4140
public class TestObserverTest extends RxJavaTest {
4241

43-
@Rule
44-
public ExpectedException thrown = ExpectedException.none();
45-
4642
@Test
4743
public void assertTestObserver() {
4844
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
@@ -56,50 +52,44 @@ public void assertTestObserver() {
5652

5753
@Test
5854
public void assertNotMatchCount() {
59-
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
60-
TestSubscriber<Integer> subscriber = new TestSubscriber<>();
61-
oi.subscribe(subscriber);
62-
63-
thrown.expect(AssertionError.class);
64-
// FIXME different message format
65-
// thrown.expectMessage("Number of items does not match. Provided: 1 Actual: 2");
66-
67-
subscriber.assertValue(1);
68-
subscriber.assertValueCount(2);
69-
subscriber.assertComplete().assertNoErrors();
55+
assertThrows(AssertionError.class, () -> {
56+
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
57+
TestSubscriber<Integer> subscriber = new TestSubscriber<>();
58+
oi.subscribe(subscriber);
59+
60+
subscriber.assertValue(1);
61+
subscriber.assertValueCount(2);
62+
subscriber.assertComplete().assertNoErrors();
63+
});
7064
}
7165

7266
@Test
7367
public void assertNotMatchValue() {
74-
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
75-
TestSubscriber<Integer> subscriber = new TestSubscriber<>();
76-
oi.subscribe(subscriber);
77-
78-
thrown.expect(AssertionError.class);
79-
// FIXME different message format
80-
// thrown.expectMessage("Value at index: 1 expected to be [3] (Integer) but was: [2] (Integer)");
81-
82-
subscriber.assertValues(1, 3);
83-
subscriber.assertValueCount(2);
84-
subscriber.assertComplete().assertNoErrors();
68+
assertThrows(AssertionError.class, () -> {
69+
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
70+
TestSubscriber<Integer> subscriber = new TestSubscriber<>();
71+
oi.subscribe(subscriber);
72+
73+
subscriber.assertValues(1, 3);
74+
subscriber.assertValueCount(2);
75+
subscriber.assertComplete().assertNoErrors();
76+
});
8577
}
8678

8779
@Test
8880
public void assertTerminalEventNotReceived() {
89-
PublishProcessor<Integer> p = PublishProcessor.create();
90-
TestSubscriber<Integer> subscriber = new TestSubscriber<>();
91-
p.subscribe(subscriber);
92-
93-
p.onNext(1);
94-
p.onNext(2);
81+
assertThrows(AssertionError.class, () -> {
82+
PublishProcessor<Integer> p = PublishProcessor.create();
83+
TestSubscriber<Integer> subscriber = new TestSubscriber<>();
84+
p.subscribe(subscriber);
9585

96-
thrown.expect(AssertionError.class);
97-
// FIXME different message format
98-
// thrown.expectMessage("No terminal events received.");
86+
p.onNext(1);
87+
p.onNext(2);
9988

100-
subscriber.assertValues(1, 2);
101-
subscriber.assertValueCount(2);
102-
subscriber.assertComplete().assertNoErrors();
89+
subscriber.assertValues(1, 2);
90+
subscriber.assertValueCount(2);
91+
subscriber.assertComplete().assertNoErrors();
92+
});
10393
}
10494

10595
@Test
@@ -853,16 +843,16 @@ public void errorMeansDisposed() {
853843

854844
@Test
855845
public void assertValuePredicateEmpty() {
856-
TestObserver<Object> to = new TestObserver<>();
846+
assertThrows("No values", AssertionError.class, () -> {
847+
TestObserver<Object> to = new TestObserver<>();
857848

858-
Observable.empty().subscribe(to);
849+
Observable.empty().subscribe(to);
859850

860-
thrown.expect(AssertionError.class);
861-
thrown.expectMessage("No values");
862-
to.assertValue(new Predicate<Object>() {
863-
@Override public boolean test(final Object o) throws Exception {
864-
return false;
865-
}
851+
to.assertValue(new Predicate<Object>() {
852+
@Override public boolean test(final Object o) throws Exception {
853+
return false;
854+
}
855+
});
866856
});
867857
}
868858

@@ -881,46 +871,46 @@ public void assertValuePredicateMatch() {
881871

882872
@Test
883873
public void assertValuePredicateNoMatch() {
884-
TestObserver<Integer> to = new TestObserver<>();
874+
assertThrows("Value not present", AssertionError.class, () -> {
875+
TestObserver<Integer> to = new TestObserver<>();
885876

886-
Observable.just(1).subscribe(to);
877+
Observable.just(1).subscribe(to);
887878

888-
thrown.expect(AssertionError.class);
889-
thrown.expectMessage("Value not present");
890-
to.assertValue(new Predicate<Integer>() {
891-
@Override public boolean test(final Integer o) throws Exception {
892-
return o != 1;
893-
}
879+
to.assertValue(new Predicate<Integer>() {
880+
@Override public boolean test(final Integer o) throws Exception {
881+
return o != 1;
882+
}
883+
});
894884
});
895885
}
896886

897887
@Test
898888
public void assertValuePredicateMatchButMore() {
899-
TestObserver<Integer> to = new TestObserver<>();
889+
assertThrows("Value present but other values as well", AssertionError.class, () -> {
890+
TestObserver<Integer> to = new TestObserver<>();
900891

901-
Observable.just(1, 2).subscribe(to);
892+
Observable.just(1, 2).subscribe(to);
902893

903-
thrown.expect(AssertionError.class);
904-
thrown.expectMessage("Value present but other values as well");
905-
to.assertValue(new Predicate<Integer>() {
906-
@Override public boolean test(final Integer o) throws Exception {
907-
return o == 1;
908-
}
894+
to.assertValue(new Predicate<Integer>() {
895+
@Override public boolean test(final Integer o) throws Exception {
896+
return o == 1;
897+
}
898+
});
909899
});
910900
}
911901

912902
@Test
913903
public void assertValueAtPredicateEmpty() {
914-
TestObserver<Object> to = new TestObserver<>();
904+
assertThrows("No values", AssertionError.class, () -> {
905+
TestObserver<Object> to = new TestObserver<>();
915906

916-
Observable.empty().subscribe(to);
907+
Observable.empty().subscribe(to);
917908

918-
thrown.expect(AssertionError.class);
919-
thrown.expectMessage("No values");
920-
to.assertValueAt(0, new Predicate<Object>() {
921-
@Override public boolean test(final Object o) throws Exception {
922-
return false;
923-
}
909+
to.assertValueAt(0, new Predicate<Object>() {
910+
@Override public boolean test(final Object o) throws Exception {
911+
return false;
912+
}
913+
});
924914
});
925915
}
926916

@@ -939,43 +929,43 @@ public void assertValueAtPredicateMatch() {
939929

940930
@Test
941931
public void assertValueAtPredicateNoMatch() {
942-
TestObserver<Integer> to = new TestObserver<>();
932+
assertThrows("Value not present", AssertionError.class, () -> {
933+
TestObserver<Integer> to = new TestObserver<>();
943934

944-
Observable.just(1, 2, 3).subscribe(to);
935+
Observable.just(1, 2, 3).subscribe(to);
945936

946-
thrown.expect(AssertionError.class);
947-
thrown.expectMessage("Value not present");
948-
to.assertValueAt(2, new Predicate<Integer>() {
949-
@Override public boolean test(final Integer o) throws Exception {
950-
return o != 3;
951-
}
937+
to.assertValueAt(2, new Predicate<Integer>() {
938+
@Override public boolean test(final Integer o) throws Exception {
939+
return o != 3;
940+
}
941+
});
952942
});
953943
}
954944

955945
@Test
956946
public void assertValueAtInvalidIndex() {
957-
TestObserver<Integer> to = new TestObserver<>();
947+
assertThrows("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> {
948+
TestObserver<Integer> to = new TestObserver<>();
958949

959-
Observable.just(1, 2).subscribe(to);
950+
Observable.just(1, 2).subscribe(to);
960951

961-
thrown.expect(AssertionError.class);
962-
thrown.expectMessage("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)");
963-
to.assertValueAt(2, new Predicate<Integer>() {
964-
@Override public boolean test(final Integer o) throws Exception {
965-
return o == 1;
966-
}
952+
to.assertValueAt(2, new Predicate<Integer>() {
953+
@Override public boolean test(final Integer o) throws Exception {
954+
return o == 1;
955+
}
956+
});
967957
});
968958
}
969959

970960
@Test
971961
public void assertValueAtIndexEmpty() {
972-
TestObserver<Object> to = new TestObserver<>();
962+
assertThrows("No values", AssertionError.class, () -> {
963+
TestObserver<Object> to = new TestObserver<>();
973964

974-
Observable.empty().subscribe(to);
965+
Observable.empty().subscribe(to);
975966

976-
thrown.expect(AssertionError.class);
977-
thrown.expectMessage("No values");
978-
to.assertValueAt(0, "a");
967+
to.assertValueAt(0, "a");
968+
});
979969
}
980970

981971
@Test
@@ -989,24 +979,24 @@ public void assertValueAtIndexMatch() {
989979

990980
@Test
991981
public void assertValueAtIndexNoMatch() {
992-
TestObserver<String> to = new TestObserver<>();
982+
assertThrows("expected: b (class: String) but was: c (class: String) (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> {
983+
TestObserver<String> to = new TestObserver<>();
993984

994-
Observable.just("a", "b", "c").subscribe(to);
985+
Observable.just("a", "b", "c").subscribe(to);
995986

996-
thrown.expect(AssertionError.class);
997-
thrown.expectMessage("expected: b (class: String) but was: c (class: String) (latch = 0, values = 3, errors = 0, completions = 1)");
998-
to.assertValueAt(2, "b");
987+
to.assertValueAt(2, "b");
988+
});
999989
}
1000990

1001991
@Test
1002992
public void assertValueAtIndexInvalidIndex() {
1003-
TestObserver<String> to = new TestObserver<>();
993+
assertThrows("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> {
994+
TestObserver<String> to = new TestObserver<>();
1004995

1005-
Observable.just("a", "b").subscribe(to);
996+
Observable.just("a", "b").subscribe(to);
1006997

1007-
thrown.expect(AssertionError.class);
1008-
thrown.expectMessage("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)");
1009-
to.assertValueAt(2, "c");
998+
to.assertValueAt(2, "c");
999+
});
10101000
}
10111001

10121002
@Test

0 commit comments

Comments
 (0)