I'm trying to use ErrorCollector
in a unit test so that an assertion failure does not abort the test. Instead, I want to check the number of failures at the end of the test and only let the test fail if that number is greater than zero.
So my basic test looks like this:
@RunWith(AndroidJUnit4.class)
public class SampleAndroidTest {
@Rule
public ErrorCollector errors = new ErrorCollector();
@Test
public void testErrorCollector() throws Exception {
try {
assertEquals(1, 1); // should pass
} catch (Throwable t) {
errors.addError(t);
}
try {
assertEquals(1, 2); // should fail
} catch (Throwable t) {
errors.addError(t);
}
try {
assertEquals(1, 3); // should fail
} catch (Throwable t) {
errors.addError(t);
}
}
}
However, I'm not sure what to do with the errors
object. The Javadocs for ErrorCollector
do not indicate any methods to get the number of failures, etc. With the current approach, only the last failure is being shown.
I tried converting the object to a String
by using toString()
, but that only gives me a memory address and not any of the other stack traces. I'm sure there is a way to get the other stack traces and that the solution is simple, but I can't figure it out for the life of me.
After doing some more research, I found out that ErrorCollector
actually prints all the failures at the end of the test. The reason that only the last failure was shown was due to the way Android Studio is designed. To get the other stack traces, one will have to view the Logcat output directly.