diff --git a/src/main/java/io/reactivex/observers/BaseTestConsumer.java b/src/main/java/io/reactivex/observers/BaseTestConsumer.java index 9d94b662cd..a0f1d9fcc2 100644 --- a/src/main/java/io/reactivex/observers/BaseTestConsumer.java +++ b/src/main/java/io/reactivex/observers/BaseTestConsumer.java @@ -20,6 +20,7 @@ import io.reactivex.disposables.Disposable; import io.reactivex.exceptions.CompositeException; import io.reactivex.functions.Predicate; +import io.reactivex.internal.functions.Functions; import io.reactivex.internal.functions.ObjectHelper; import io.reactivex.internal.util.ExceptionHelper; @@ -227,18 +228,7 @@ public final U assertNoErrors() { */ @SuppressWarnings("unchecked") public final U assertError(Throwable error) { - int s = errors.size(); - if (s == 0) { - throw fail("No errors"); - } - if (errors.contains(error)) { - if (s != 1) { - throw fail("Error present but other errors as well"); - } - } else { - throw fail("Error not present"); - } - return (U)this; + return (U)assertError(Functions.equalsWith(error)); } /** @@ -249,28 +239,7 @@ public final U assertError(Throwable error) { */ @SuppressWarnings("unchecked") public final U assertError(Class errorClass) { - int s = errors.size(); - if (s == 0) { - throw fail("No errors"); - } - - boolean found = false; - - for (Throwable e : errors) { - if (errorClass.isInstance(e)) { - found = true; - break; - } - } - - if (found) { - if (s != 1) { - throw fail("Error present but other errors as well"); - } - } else { - throw fail("Error not present"); - } - return (U)this; + return (U)assertError((Predicate)Functions.isInstanceOf(errorClass)); } /** diff --git a/src/test/java/io/reactivex/observers/TestObserverTest.java b/src/test/java/io/reactivex/observers/TestObserverTest.java index 6e72632c4c..03b0e6ca6b 100644 --- a/src/test/java/io/reactivex/observers/TestObserverTest.java +++ b/src/test/java/io/reactivex/observers/TestObserverTest.java @@ -295,10 +295,10 @@ public void createDelegate() { // expected } - ts.assertValueSequence(Arrays.asList(1)); + ts.assertValueSequence(Collections.singletonList(1)); try { - ts.assertValueSequence(Arrays.asList(2)); + ts.assertValueSequence(Collections.singletonList(2)); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { // expected @@ -344,21 +344,21 @@ public void assertError() { ts.assertErrorMessage(""); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { - + // expected } try { ts.assertSubscribed(); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { - + // expected } try { ts.assertTerminated(); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { - + // expected } ts.onSubscribe(Disposables.empty()); @@ -390,7 +390,7 @@ public boolean test(Throwable t) throws Exception { ts.assertErrorMessage(""); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { - + // expected } try { @@ -671,11 +671,11 @@ public void onNext() { assertEquals(0, ts.valueCount()); - assertEquals(Arrays.asList(), ts.values()); + assertEquals(Collections.emptyList(), ts.values()); ts.onNext(1); - assertEquals(Arrays.asList(1), ts.values()); + assertEquals(Collections.singletonList(1), ts.values()); ts.cancel(); @@ -684,11 +684,11 @@ public void onNext() { ts.assertValue(1); - assertEquals(Arrays.asList(Arrays.asList(1), Collections.emptyList(), Collections.emptyList()), ts.getEvents()); + assertEquals(Arrays.asList(Collections.singletonList(1), Collections.emptyList(), Collections.emptyList()), ts.getEvents()); ts.onComplete(); - assertEquals(Arrays.asList(Arrays.asList(1), Collections.emptyList(), Collections.singletonList(Notification.createOnComplete())), ts.getEvents()); + assertEquals(Arrays.asList(Collections.singletonList(1), Collections.emptyList(), Collections.singletonList(Notification.createOnComplete())), ts.getEvents()); } @Test @@ -896,14 +896,14 @@ public void assertValueSequence() { ts.onNext(2); try { - ts.assertValueSequence(Arrays.asList()); + ts.assertValueSequence(Collections.emptyList()); throw new RuntimeException("Should have thrown"); } catch (AssertionError ex) { // expected } try { - ts.assertValueSequence(Arrays.asList(1)); + ts.assertValueSequence(Collections.singletonList(1)); throw new RuntimeException("Should have thrown"); } catch (AssertionError ex) { // expected diff --git a/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java b/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java index 4878247b99..ecdf1950eb 100644 --- a/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java +++ b/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java @@ -738,10 +738,10 @@ public void createDelegate() { // expected } - ts.assertValueSequence(Arrays.asList(1)); + ts.assertValueSequence(Collections.singletonList(1)); try { - ts.assertValueSequence(Arrays.asList(2)); + ts.assertValueSequence(Collections.singletonList(2)); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { // expected @@ -780,7 +780,7 @@ public void assertError() { ts.assertErrorMessage(""); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { - + // expected } try { @@ -794,14 +794,14 @@ public void assertError() { ts.assertSubscribed(); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { - + // expected } try { ts.assertTerminated(); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { - + // expected } ts.onSubscribe(new BooleanSubscription()); @@ -833,7 +833,7 @@ public boolean test(Throwable t) { ts.assertErrorMessage(""); throw new RuntimeException("Should have thrown"); } catch (AssertionError exc) { - + // expected } try { @@ -1113,11 +1113,11 @@ public void onNext() { assertEquals(0, ts.valueCount()); - assertEquals(Arrays.asList(), ts.values()); + assertEquals(Collections.emptyList(), ts.values()); ts.onNext(1); - assertEquals(Arrays.asList(1), ts.values()); + assertEquals(Collections.singletonList(1), ts.values()); ts.cancel(); @@ -1126,11 +1126,11 @@ public void onNext() { ts.assertValue(1); - assertEquals(Arrays.asList(Arrays.asList(1), Collections.emptyList(), Collections.emptyList()), ts.getEvents()); + assertEquals(Arrays.asList(Collections.singletonList(1), Collections.emptyList(), Collections.emptyList()), ts.getEvents()); ts.onComplete(); - assertEquals(Arrays.asList(Arrays.asList(1), Collections.emptyList(), Collections.singletonList(Notification.createOnComplete())), ts.getEvents()); + assertEquals(Arrays.asList(Collections.singletonList(1), Collections.emptyList(), Collections.singletonList(Notification.createOnComplete())), ts.getEvents()); } @Test @@ -1338,14 +1338,14 @@ public void assertValueSequence() { ts.onNext(2); try { - ts.assertValueSequence(Arrays.asList()); + ts.assertValueSequence(Collections.emptyList()); throw new RuntimeException("Should have thrown"); } catch (AssertionError ex) { // expected } try { - ts.assertValueSequence(Arrays.asList(1)); + ts.assertValueSequence(Collections.singletonList(1)); throw new RuntimeException("Should have thrown"); } catch (AssertionError ex) { // expected