简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM