In this example, we take a look at a JUnit example of failing a test on NullPointerException.
Code Example:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
public class JunitExceptionExample {
@Test
public void test() {
try {
String str = null;
str.toLowerCase();
} catch (NullPointerException nullPointerException) {
assertThrows(NullPointerException.class, () -> {
throw nullPointerException;
});
} catch (Exception exception) {
fail("Expected NullPointerException, but caught: " + exception.getClass().getSimpleName());
}
}
}
In the above example, we have made use of the assertThrows method from org.junit.jupiter.api.Assertions class of Junit 5.
For this example: The method asserts that execution of the supplied executable throws an exception of the expectedType (NullPointerException) and returns the exception. If there is no exception thrown by your code, or a different expectation other than NullPointerException is thrown assertThrows method will fail.
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Free Unlimited Calls from MTNL & BSNL Landlines from 1st May 2015 - HowTos
- How to make EditText text to uppercase or lowercase on macOS - MacOS
- Auto Execute a command or script on Mac Terminal Startup - MacOS
- [Nopepad++] How to add text at end of each line - NotepadPlusPlus
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server - Java
- Android Images with Rounded Corners : ImageView - Android
- Fix MySQL ERROR 1045 (28000): Access denied for user root@localhost (using password: YES) - MySQL
- List of 32 CSS cursors web developers must know about - CSS