How to always fail a Junit Java Test Case

If you want to always intentionally fail a Java Junit test case then make use of the static fail() method from the org.junit.jupiter.api.Assertions class.

import static org.junit.jupiter.api.Assertions.fail;


public class TestJunit {

    @Test
    public void failTest() {
        System.out.println("hello there!");
        fail("This case will always fail!");
    }
}
org.opentest4j.AssertionFailedError: This case will always fail!

	at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:38)
	at org.junit.jupiter.api.Assertions.fail(Assertions.java:135)
	at TestJunit.failTest(TestJunit.java:11)
	...
        ...
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Always fail JUnit Test Case

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!