📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Check out JUnit 5 tutorials and examples at https://www.javaguides.net/p/junit-5.html
void org.junit.Assert.fail(String message)
- message the identifying message for the AssertionError (null okay)
Assert.fail(String message) Method Example
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class AssertFailExample {
public int largest(final int[] list) {
int index, max = Integer.MAX_VALUE;
for (index = 0; index < list.length - 1; index++) {
if (list[index] > max) {
max = list[index];
}
}
return max;
}
Let's write the JUnit test for above largest(final int[] list) method:@Test
public void testEmpty() {
try {
largest(new int[] {});
fail("Should have thrown an exception");
} catch (final RuntimeException e) {
assertTrue(true);
}
}
}
Output:
Related JUnit Examples
- JUnit Assert.assertArrayEquals() Method Example
- JUnit Assert.assertEquals() Method Example
- JUnit Assert.assertTrue() Method Example
- JUnit Assert.assertFalse() Method Example
- JUnit Assert.assertNull() Method Example
- JUnit Assert.assertNotNull() Method Example
- JUnit Assert.assertSame() Method Example
- JUnit Assert.assertNotSame() Method Example
- JUnit Assert.fail() Method Example
Comments
Post a Comment
Leave Comment