0

When the org.junit.runner.notification.Failure's object getException() method return value is != null, is that equivalent to an Error (as opposed to a Failure) in Eclipse's JUnit view?

I have a JUnit4 test

public class NewTests
{
    @Test
    public void SampleTestPass()
    {
        org.junit.Assert.assertTrue(true);
    }
    @Test
    public void SampleTestFail()
    {
        org.junit.Assert.assertTrue(false);
    }
    @Test
    public void SampleTestError()
    {
        throw new Exception();
    }
}

which I bundle into a suite with

import org.junit.runners.Suite;
import org.junit.runner.RunWith;

@RunWith(Suite.class)
@Suite.SuiteClasses({NewTests.class})
public class SampleTestSuite {

}

this is executed by:

Result result = JUnitCore.runClasses(SampleTestSuite.class);
if(result.wasSuccessful())
{
    output = "passed";
}
else if(result.getFailureCount != 0)
{
    output = "failed";
}

how do I distinguish the third case where the test results in an error?

When I run the JUnit tests from Eclipse I am presented with a pass for the first test, a failure for the second test and an error for the third test.

When using JUnit3 and running my tests with

TestSuite suite = new TestSuite(NewTests.class);
TestResult result = new TestResult();
suite.run(result);

I could use the junit.framework.TestResult class and call failureCount() and errorCount() to get all the information I needed.

The only similar thing I could find was that inside the JUnit4 result object there is a org.junit.runner.notification.Failure object that can contain the thrown exception returned from getException() which returns a throwable. If this is populated is this the equivalent to the JUnit3 result.errorCount and so can I assume that the thrid (error) case is satisfied iff there is a throwable returned from org.junit.runner.notification.Failure.getException()?

Encaitar
  • 398
  • 5
  • 16
  • If I recall correctly you could indicate in annotations that a method expects an error. Take a look at http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests?rq=1 – skiwi Jan 29 '14 at 11:32
  • Thank you skiwi. Annotating with @Test(expected=SomeExceptionClass.class) allows you to check for expected errors. I want to be able to tell when I have an unexpected error, rather than an unexpected failure. – Encaitar Jan 29 '14 at 11:36
  • A Failure object also knows an exception (method getException()). Maybe this is the error indicator? [I do not know, how this handles the expected exceptions, but maybe you could try out.] – Seelenvirtuose Jan 29 '14 at 13:01
  • Thank you @Seelenvirtuose. I have tried that and I am able to identify when an exception is thrown using getExecption(). Do you know if this is the only case (throwing an uncaught exception) that JUnit4 test cases normally report Error as opposed to Failure? – Encaitar Jan 29 '14 at 13:21
  • @Seelenvirtuose it seems that getException can return any throwable object and an AssertationError is a subclass of throwable. I can check for AssertationErrors with instanceOf but it at least shows that this is not equivalent to Error vs Failure in JUnit3. – Encaitar Jan 29 '14 at 14:46
  • Sorry, I do not know, if the expected exceptions (which even should result in success) and the assertion error will be returned by the method getException(). My expectation is, that they are not, which would be great. You must try it out yourself. – Seelenvirtuose Jan 29 '14 at 15:30

0 Answers0