20
20
import java .util .*;
21
21
import java .util .concurrent .TimeUnit ;
22
22
23
- import org .junit .*;
24
- import org .junit .rules .ExpectedException ;
23
+ import org .junit .Test ;
25
24
import org .mockito .InOrder ;
26
25
import org .reactivestreams .Subscriber ;
27
26
28
27
import io .reactivex .rxjava3 .core .*;
29
28
import io .reactivex .rxjava3 .core .Observable ;
30
29
import io .reactivex .rxjava3 .core .Observer ;
31
- import io .reactivex .rxjava3 .disposables .* ;
30
+ import io .reactivex .rxjava3 .disposables .Disposable ;
32
31
import io .reactivex .rxjava3 .exceptions .TestException ;
33
32
import io .reactivex .rxjava3 .functions .Predicate ;
34
33
import io .reactivex .rxjava3 .internal .functions .Functions ;
40
39
41
40
public class TestObserverTest extends RxJavaTest {
42
41
43
- @ Rule
44
- public ExpectedException thrown = ExpectedException .none ();
45
-
46
42
@ Test
47
43
public void assertTestObserver () {
48
44
Flowable <Integer > oi = Flowable .fromIterable (Arrays .asList (1 , 2 ));
@@ -56,50 +52,44 @@ public void assertTestObserver() {
56
52
57
53
@ Test
58
54
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
+ });
70
64
}
71
65
72
66
@ Test
73
67
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
+ });
85
77
}
86
78
87
79
@ Test
88
80
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 );
95
85
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 );
99
88
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
+ });
103
93
}
104
94
105
95
@ Test
@@ -853,16 +843,16 @@ public void errorMeansDisposed() {
853
843
854
844
@ Test
855
845
public void assertValuePredicateEmpty () {
856
- TestObserver <Object > to = new TestObserver <>();
846
+ assertThrows ("No values" , AssertionError .class , () -> {
847
+ TestObserver <Object > to = new TestObserver <>();
857
848
858
- Observable .empty ().subscribe (to );
849
+ Observable .empty ().subscribe (to );
859
850
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
+ });
866
856
});
867
857
}
868
858
@@ -881,46 +871,46 @@ public void assertValuePredicateMatch() {
881
871
882
872
@ Test
883
873
public void assertValuePredicateNoMatch () {
884
- TestObserver <Integer > to = new TestObserver <>();
874
+ assertThrows ("Value not present" , AssertionError .class , () -> {
875
+ TestObserver <Integer > to = new TestObserver <>();
885
876
886
- Observable .just (1 ).subscribe (to );
877
+ Observable .just (1 ).subscribe (to );
887
878
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
+ });
894
884
});
895
885
}
896
886
897
887
@ Test
898
888
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 <>();
900
891
901
- Observable .just (1 , 2 ).subscribe (to );
892
+ Observable .just (1 , 2 ).subscribe (to );
902
893
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
+ });
909
899
});
910
900
}
911
901
912
902
@ Test
913
903
public void assertValueAtPredicateEmpty () {
914
- TestObserver <Object > to = new TestObserver <>();
904
+ assertThrows ("No values" , AssertionError .class , () -> {
905
+ TestObserver <Object > to = new TestObserver <>();
915
906
916
- Observable .empty ().subscribe (to );
907
+ Observable .empty ().subscribe (to );
917
908
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
+ });
924
914
});
925
915
}
926
916
@@ -939,43 +929,43 @@ public void assertValueAtPredicateMatch() {
939
929
940
930
@ Test
941
931
public void assertValueAtPredicateNoMatch () {
942
- TestObserver <Integer > to = new TestObserver <>();
932
+ assertThrows ("Value not present" , AssertionError .class , () -> {
933
+ TestObserver <Integer > to = new TestObserver <>();
943
934
944
- Observable .just (1 , 2 , 3 ).subscribe (to );
935
+ Observable .just (1 , 2 , 3 ).subscribe (to );
945
936
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
+ });
952
942
});
953
943
}
954
944
955
945
@ Test
956
946
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 <>();
958
949
959
- Observable .just (1 , 2 ).subscribe (to );
950
+ Observable .just (1 , 2 ).subscribe (to );
960
951
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
+ });
967
957
});
968
958
}
969
959
970
960
@ Test
971
961
public void assertValueAtIndexEmpty () {
972
- TestObserver <Object > to = new TestObserver <>();
962
+ assertThrows ("No values" , AssertionError .class , () -> {
963
+ TestObserver <Object > to = new TestObserver <>();
973
964
974
- Observable .empty ().subscribe (to );
965
+ Observable .empty ().subscribe (to );
975
966
976
- thrown .expect (AssertionError .class );
977
- thrown .expectMessage ("No values" );
978
- to .assertValueAt (0 , "a" );
967
+ to .assertValueAt (0 , "a" );
968
+ });
979
969
}
980
970
981
971
@ Test
@@ -989,24 +979,24 @@ public void assertValueAtIndexMatch() {
989
979
990
980
@ Test
991
981
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 <>();
993
984
994
- Observable .just ("a" , "b" , "c" ).subscribe (to );
985
+ Observable .just ("a" , "b" , "c" ).subscribe (to );
995
986
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
+ });
999
989
}
1000
990
1001
991
@ Test
1002
992
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 <>();
1004
995
1005
- Observable .just ("a" , "b" ).subscribe (to );
996
+ Observable .just ("a" , "b" ).subscribe (to );
1006
997
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
+ });
1010
1000
}
1011
1001
1012
1002
@ Test
0 commit comments