Skip to content

Commit a16f63f

Browse files
authored
2.x: Clarify TestObserver.assertValueSet in docs and via tests (#6152)
* 2.x: Clarify TestObserver.assertValueSet in docs and via tests * Grammar.
1 parent 0e7b8ea commit a16f63f

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

src/main/java/io/reactivex/observers/BaseTestConsumer.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,14 @@ public final U assertValuesOnly(T... values) {
565565
}
566566

567567
/**
568-
* Assert that the TestObserver/TestSubscriber received only the specified values in any order.
569-
* <p>This helps asserting when the order of the values is not guaranteed, i.e., when merging
568+
* Assert that the TestObserver/TestSubscriber received only items that are in the specified
569+
* collection as well, irrespective of the order they were received.
570+
* <p>
571+
* This helps asserting when the order of the values is not guaranteed, i.e., when merging
570572
* asynchronous streams.
573+
* <p>
574+
* To ensure that only the expected items have been received, no more and no less, in any order,
575+
* apply {@link #assertValueCount(int)} with {@code expected.size()}.
571576
*
572577
* @param expected the collection of values expected in any order
573578
* @return this

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

+34
Original file line numberDiff line numberDiff line change
@@ -1626,4 +1626,38 @@ public void assertValueSequenceOnlyThrowsWhenErrored() {
16261626
// expected
16271627
}
16281628
}
1629+
1630+
@Test
1631+
public void assertValueSetWiderSet() {
1632+
Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7));
1633+
1634+
Observable.just(4, 5, 1, 3, 2)
1635+
.test()
1636+
.assertValueSet(set);
1637+
}
1638+
1639+
@Test
1640+
public void assertValueSetExact() {
1641+
Set<Integer> set = new HashSet<Integer>(Arrays.asList(1, 2, 3, 4, 5));
1642+
1643+
Observable.just(4, 5, 1, 3, 2)
1644+
.test()
1645+
.assertValueSet(set)
1646+
.assertValueCount(set.size());
1647+
}
1648+
1649+
@Test
1650+
public void assertValueSetMissing() {
1651+
Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 4, 5, 6, 7));
1652+
1653+
try {
1654+
Observable.range(1, 5)
1655+
.test()
1656+
.assertValueSet(set);
1657+
1658+
throw new RuntimeException("Should have failed");
1659+
} catch (AssertionError ex) {
1660+
assertTrue(ex.getMessage(), ex.getMessage().contains("Value not in the expected collection: " + 3));
1661+
}
1662+
}
16291663
}

src/test/java/io/reactivex/subscribers/TestSubscriberTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -2186,4 +2186,38 @@ public void awaitCount0() {
21862186
TestSubscriber<Integer> ts = TestSubscriber.create();
21872187
ts.awaitCount(0, TestWaitStrategy.SLEEP_1MS, 0);
21882188
}
2189+
2190+
@Test
2191+
public void assertValueSetWiderSet() {
2192+
Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7));
2193+
2194+
Flowable.just(4, 5, 1, 3, 2)
2195+
.test()
2196+
.assertValueSet(set);
2197+
}
2198+
2199+
@Test
2200+
public void assertValueSetExact() {
2201+
Set<Integer> set = new HashSet<Integer>(Arrays.asList(1, 2, 3, 4, 5));
2202+
2203+
Flowable.just(4, 5, 1, 3, 2)
2204+
.test()
2205+
.assertValueSet(set)
2206+
.assertValueCount(set.size());
2207+
}
2208+
2209+
@Test
2210+
public void assertValueSetMissing() {
2211+
Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 4, 5, 6, 7));
2212+
2213+
try {
2214+
Flowable.range(1, 5)
2215+
.test()
2216+
.assertValueSet(set);
2217+
2218+
throw new RuntimeException("Should have failed");
2219+
} catch (AssertionError ex) {
2220+
assertTrue(ex.getMessage(), ex.getMessage().contains("Value not in the expected collection: " + 3));
2221+
}
2222+
}
21892223
}

0 commit comments

Comments
 (0)