Android JUnit: How to have an exception cause a test case to pass (@Test annotation)
I'm trying to write some JUnit tests for an Android application.
I've read online that to have a unit test pass if it throws an exception开发者_如何学C, you would use the @Test annotation like this
@Test(expected = NullPointerException.class)
public void testNullValue() throws Throwable
{
Object o = null;
o.toString();
}
but Eclipse is telling me that this annotation doesn't exist. How can I fix this? If I run the test, it runs fine and fails as expected but obviously I want it to fail (and thus actually pass) :)
You can always bypass it manually:
public void testNullValue()
{
try {
Object o = null;
o.toString();
fail("Expected NullPointerException to be thrown");
} catch (NullPointerException e) {
assertTrue(true);
}
}
I believe that should be:
@Test(expected=NullPointerException.class)
- Double check the JUnit version you are using
- Do not use the JUnit that eclipse provides (Indigo) but import manually a JUnit 4.9 manually
The error is that "mysterious" error that has not an easy or immediate answer, I am only trying ideas
精彩评论