Skip to content

Commit 994c65d

Browse files
vanniktechakarnokd
authored andcommitted
2.x: Add assertValuesOnly to BaseTestConsumer. (#5568)
* 2.x: Add assertValuesOnly to BaseTestConsumer. * Address comments.
1 parent ea7ca2c commit 994c65d

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,21 @@ public final U assertValues(T... values) {
522522
return (U)this;
523523
}
524524

525+
/**
526+
* Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating.
527+
* @param values the values expected
528+
* @return this;
529+
* @since 2.1.4
530+
*/
531+
@SuppressWarnings("unchecked")
532+
@Experimental
533+
public final U assertValuesOnly(T... values) {
534+
return assertSubscribed()
535+
.assertValues(values)
536+
.assertNoErrors()
537+
.assertNotComplete();
538+
}
539+
525540
/**
526541
* Assert that the TestObserver/TestSubscriber received only the specified values in any order.
527542
* <p>This helps asserting when the order of the values is not guaranteed, i.e., when merging

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,4 +1439,66 @@ public void withTag() {
14391439
assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
14401440
}
14411441
}
1442+
1443+
@Test
1444+
public void assertValuesOnly() {
1445+
TestObserver<Integer> to = TestObserver.create();
1446+
to.onSubscribe(Disposables.empty());
1447+
to.assertValuesOnly();
1448+
1449+
to.onNext(5);
1450+
to.assertValuesOnly(5);
1451+
1452+
to.onNext(-1);
1453+
to.assertValuesOnly(5, -1);
1454+
}
1455+
1456+
@Test
1457+
public void assertValuesOnlyThrowsOnUnexpectedValue() {
1458+
TestObserver<Integer> to = TestObserver.create();
1459+
to.onSubscribe(Disposables.empty());
1460+
to.assertValuesOnly();
1461+
1462+
to.onNext(5);
1463+
to.assertValuesOnly(5);
1464+
1465+
to.onNext(-1);
1466+
1467+
try {
1468+
to.assertValuesOnly(5);
1469+
fail();
1470+
} catch (AssertionError ex) {
1471+
// expected
1472+
}
1473+
}
1474+
1475+
@Test
1476+
public void assertValuesOnlyThrowsWhenCompleted() {
1477+
TestObserver<Integer> to = TestObserver.create();
1478+
to.onSubscribe(Disposables.empty());
1479+
1480+
to.onComplete();
1481+
1482+
try {
1483+
to.assertValuesOnly();
1484+
fail();
1485+
} catch (AssertionError ex) {
1486+
// expected
1487+
}
1488+
}
1489+
1490+
@Test
1491+
public void assertValuesOnlyThrowsWhenErrored() {
1492+
TestObserver<Integer> to = TestObserver.create();
1493+
to.onSubscribe(Disposables.empty());
1494+
1495+
to.onError(new TestException());
1496+
1497+
try {
1498+
to.assertValuesOnly();
1499+
fail();
1500+
} catch (AssertionError ex) {
1501+
// expected
1502+
}
1503+
}
14421504
}

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,4 +1997,66 @@ public void waitStrategyRuns() {
19971997
ws.run();
19981998
}
19991999
}
2000+
2001+
@Test
2002+
public void assertValuesOnly() {
2003+
TestSubscriber<Integer> ts = TestSubscriber.create();
2004+
ts.onSubscribe(new BooleanSubscription());
2005+
ts.assertValuesOnly();
2006+
2007+
ts.onNext(5);
2008+
ts.assertValuesOnly(5);
2009+
2010+
ts.onNext(-1);
2011+
ts.assertValuesOnly(5, -1);
2012+
}
2013+
2014+
@Test
2015+
public void assertValuesOnlyThrowsOnUnexpectedValue() {
2016+
TestSubscriber<Integer> ts = TestSubscriber.create();
2017+
ts.onSubscribe(new BooleanSubscription());
2018+
ts.assertValuesOnly();
2019+
2020+
ts.onNext(5);
2021+
ts.assertValuesOnly(5);
2022+
2023+
ts.onNext(-1);
2024+
2025+
try {
2026+
ts.assertValuesOnly(5);
2027+
fail();
2028+
} catch (AssertionError ex) {
2029+
// expected
2030+
}
2031+
}
2032+
2033+
@Test
2034+
public void assertValuesOnlyThrowsWhenCompleted() {
2035+
TestSubscriber<Integer> ts = TestSubscriber.create();
2036+
ts.onSubscribe(new BooleanSubscription());
2037+
2038+
ts.onComplete();
2039+
2040+
try {
2041+
ts.assertValuesOnly();
2042+
fail();
2043+
} catch (AssertionError ex) {
2044+
// expected
2045+
}
2046+
}
2047+
2048+
@Test
2049+
public void assertValuesOnlyThrowsWhenErrored() {
2050+
TestSubscriber<Integer> ts = TestSubscriber.create();
2051+
ts.onSubscribe(new BooleanSubscription());
2052+
2053+
ts.onError(new TestException());
2054+
2055+
try {
2056+
ts.assertValuesOnly();
2057+
fail();
2058+
} catch (AssertionError ex) {
2059+
// expected
2060+
}
2061+
}
20002062
}

0 commit comments

Comments
 (0)