Krishnom
Krishnom

Reputation: 1446

Generating Failure in junit4

I am trying to understand which call causes Error and Which causes failures in Junit4. Until Junit3,

Failure can be created using

junit.framework.AssertionFailedError

And Error with

junit.framework.Assert.assertEquals

But with the deprecation of junit.framework.Assert, which is not moved to org.junit.Assert, I am not able to find a way in junit4 to throw a failure. Anything I try with org.junit.Assert (even Assert.fail() ) , JUnit considers it as Error.

Any idea on how to properly generate failures in Junit4 style tests?

Update

I figured out that there is a std.err at the end of XML generated by JUnit ant target.

<system-err>TEXT here</system-err>

and I suspected this is the cause that making it ERROR instead of Failure. But when I cleared all sys.err, it still marking it ERROR.

Upvotes: 0

Views: 269

Answers (2)

Chase f
Chase f

Reputation: 1

I am having the same issue. The only solution I have found so far is to use a try block followed by

    catch (AssertionError ae) {
       fail(ae.toString());
     }

But I can see downsides to this and I have seen many people say this is bad practice. Unfortunately I don't see another way around it when using ant to make a report.

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58892

You can still use Assert.assertThat for getting assertion failure

assertThat(0, is(1)); // fails:
assertThat(0, is(not(1))) // passes

It may not what you need, but also JUnit 4 has ComparisonFailure

Thrown when an assertEquals(String, String) fails. Create and throw a ComparisonFailure manually if you want to show users the difference between two complex strings.

Upvotes: 1

Related Questions