-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: TestSubscriber extra info on assertion failures #3934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,11 @@ | |
*/ | ||
package rx.observers; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.*; | ||
|
||
import rx.Notification; | ||
import rx.Observer; | ||
import rx.exceptions.CompositeException; | ||
|
||
/** | ||
* Observer usable for unit testing to perform assertions, inspect received events or wrap a mocked Observer. | ||
|
@@ -113,7 +112,7 @@ public List<Object> getEvents() { | |
*/ | ||
public void assertReceivedOnNext(List<T> items) { | ||
if (onNextEvents.size() != items.size()) { | ||
throw new AssertionError("Number of items does not match. Provided: " + items.size() + " Actual: " + onNextEvents.size() | ||
assertionError("Number of items does not match. Provided: " + items.size() + " Actual: " + onNextEvents.size() | ||
+ ".\n" | ||
+ "Provided values: " + items | ||
+ "\n" | ||
|
@@ -126,10 +125,10 @@ public void assertReceivedOnNext(List<T> items) { | |
if (expected == null) { | ||
// check for null equality | ||
if (actual != null) { | ||
throw new AssertionError("Value at index: " + i + " expected to be [null] but was: [" + actual + "]"); | ||
assertionError("Value at index: " + i + " expected to be [null] but was: [" + actual + "]"); | ||
} | ||
} else if (!expected.equals(actual)) { | ||
throw new AssertionError("Value at index: " + i | ||
assertionError("Value at index: " + i | ||
+ " expected to be [" + expected + "] (" + expected.getClass().getSimpleName() | ||
+ ") but was: [" + actual + "] (" + (actual != null ? actual.getClass().getSimpleName() : "null") + ")"); | ||
|
||
|
@@ -146,22 +145,54 @@ public void assertReceivedOnNext(List<T> items) { | |
*/ | ||
public void assertTerminalEvent() { | ||
if (onErrorEvents.size() > 1) { | ||
throw new AssertionError("Too many onError events: " + onErrorEvents.size()); | ||
assertionError("Too many onError events: " + onErrorEvents.size()); | ||
} | ||
|
||
if (onCompletedEvents.size() > 1) { | ||
throw new AssertionError("Too many onCompleted events: " + onCompletedEvents.size()); | ||
assertionError("Too many onCompleted events: " + onCompletedEvents.size()); | ||
} | ||
|
||
if (onCompletedEvents.size() == 1 && onErrorEvents.size() == 1) { | ||
throw new AssertionError("Received both an onError and onCompleted. Should be one or the other."); | ||
assertionError("Received both an onError and onCompleted. Should be one or the other."); | ||
} | ||
|
||
if (onCompletedEvents.size() == 0 && onErrorEvents.size() == 0) { | ||
throw new AssertionError("No terminal events received."); | ||
assertionError("No terminal events received."); | ||
} | ||
} | ||
|
||
/** | ||
* Combines an assertion error message with the current completion and error state of this | ||
* TestSubscriber, giving more information when some assertXXX check fails. | ||
* @param message the message to use for the error | ||
*/ | ||
final void assertionError(String message) { | ||
StringBuilder b = new StringBuilder(message.length() + 32); | ||
|
||
b.append(message); | ||
|
||
|
||
b.append(" ("); | ||
b.append(onCompletedEvents.size()); | ||
b.append(" completions)"); | ||
|
||
if (!onErrorEvents.isEmpty()) { | ||
b.append(" (+") | ||
.append(onErrorEvents.size()) | ||
.append(" errors)"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
} | ||
|
||
AssertionError ae = new AssertionError(b.toString()); | ||
if (!onErrorEvents.isEmpty()) { | ||
if (onErrorEvents.size() == 1) { | ||
ae.initCause(onErrorEvents.get(0)); | ||
} else { | ||
ae.initCause(new CompositeException(onErrorEvents)); | ||
} | ||
} | ||
throw ae; | ||
} | ||
|
||
// do nothing ... including swallowing errors | ||
private static Observer<Object> INERT = new Observer<Object>() { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,18 @@ | |
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.*; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.junit.*; | ||
import org.junit.rules.ExpectedException; | ||
import org.mockito.InOrder; | ||
|
||
import rx.*; | ||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.Scheduler.Worker; | ||
import rx.Subscriber; | ||
import rx.exceptions.*; | ||
import rx.functions.Action0; | ||
import rx.schedulers.Schedulers; | ||
|
@@ -610,9 +612,105 @@ public void assertValuesShouldThrowIfNumberOfItemsDoesNotMatch() { | |
} catch (AssertionError expected) { | ||
assertEquals("Number of items does not match. Provided: 2 Actual: 3.\n" + | ||
"Provided values: [1, 2]\n" + | ||
"Actual values: [a, b, c]", | ||
"Actual values: [a, b, c] (0 completions)", | ||
expected.getMessage() | ||
); | ||
} | ||
} | ||
|
||
@Test | ||
public void assertionFailureGivesActiveDetails() { | ||
TestSubscriber<String> ts = new TestSubscriber<String>(); | ||
|
||
ts.onNext("a"); | ||
ts.onNext("b"); | ||
ts.onNext("c"); | ||
ts.onError(new TestException("forced failure")); | ||
|
||
try { | ||
ts.assertValues("1", "2"); | ||
fail(); | ||
} catch (AssertionError expected) { | ||
assertEquals("Number of items does not match. Provided: 2 Actual: 3.\n" + | ||
"Provided values: [1, 2]\n" + | ||
"Actual values: [a, b, c] (0 completions) (+1 errors)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case of long There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. |
||
expected.getMessage() | ||
); | ||
Throwable ex = expected.getCause(); | ||
assertEquals(TestException.class, ex.getClass()); | ||
assertEquals("forced failure", ex.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void assertionFailureShowsMultipleErrors() { | ||
TestSubscriber<String> ts = new TestSubscriber<String>(); | ||
|
||
ts.onNext("a"); | ||
ts.onNext("b"); | ||
ts.onNext("c"); | ||
ts.onError(new TestException("forced failure")); | ||
ts.onError(new TestException("forced failure 2")); | ||
|
||
try { | ||
ts.assertValues("1", "2"); | ||
fail(); | ||
} catch (AssertionError expected) { | ||
assertEquals("Number of items does not match. Provided: 2 Actual: 3.\n" + | ||
"Provided values: [1, 2]\n" + | ||
"Actual values: [a, b, c] (0 completions) (+2 errors)", | ||
expected.getMessage() | ||
); | ||
Throwable ex = expected.getCause(); | ||
assertEquals(CompositeException.class, ex.getClass()); | ||
List<Throwable> list = ((CompositeException)ex).getExceptions(); | ||
assertEquals(2, list.size()); | ||
assertEquals("forced failure", list.get(0).getMessage()); | ||
assertEquals("forced failure 2", list.get(1).getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void assertionFailureShowsCompletion() { | ||
TestSubscriber<String> ts = new TestSubscriber<String>(); | ||
|
||
ts.onNext("a"); | ||
ts.onNext("b"); | ||
ts.onNext("c"); | ||
ts.onCompleted(); | ||
|
||
try { | ||
ts.assertValues("1", "2"); | ||
fail(); | ||
} catch (AssertionError expected) { | ||
assertEquals("Number of items does not match. Provided: 2 Actual: 3.\n" + | ||
"Provided values: [1, 2]\n" + | ||
"Actual values: [a, b, c] (1 completions)", | ||
expected.getMessage() | ||
); | ||
} | ||
} | ||
|
||
@Test | ||
public void assertionFailureShowsMultipleCompletions() { | ||
TestSubscriber<String> ts = new TestSubscriber<String>(); | ||
|
||
ts.onNext("a"); | ||
ts.onNext("b"); | ||
ts.onNext("c"); | ||
ts.onCompleted(); | ||
ts.onCompleted(); | ||
|
||
try { | ||
ts.assertValues("1", "2"); | ||
fail(); | ||
} catch (AssertionError expected) { | ||
assertEquals("Number of items does not match. Provided: 2 Actual: 3.\n" + | ||
"Provided values: [1, 2]\n" + | ||
"Actual values: [a, b, c] (2 completions)", | ||
expected.getMessage() | ||
); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TestObserver
needs tests tooThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertValues
go totestObserver.assertReceivedOnNext()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure… but it's implementation detail…