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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap