How to Create Test Cases for Exceptions in Java31 Mar 2025 | 3 min read In Java, creating test cases for exceptions is not a difficult task. The JUnit testing tool of Java provides a way to track the exception handling of code. We can write the test cases to check whether the code throws a desired exception or not. In order to write and run the test cases for an exception, we need the JUnit jar file in the system, or we need to set up the JUnit environment in our system. To test the exceptions, we should follow the following steps:
![]() Create a Class to be TestedWe first create a class that needs to be tested. We will create class ShowMessage class. In this class, we will create two methods, i.e., show() method and the finalMessage() method. Using the constructor, we set the value to the message property. In the show() method, we will print the message and generate the divide by zero exception. In the finalMessage() method, we will concatenate the given message with the "Hello!" string. The code of the above theory is given below: ShowMessage.java Create a Class for Test CaseNow, we need to write the Java class for the test case name TestShowMessage.java. In this class, we will create a msg property with a default string message and create the object of the ShowMessage class by passing that default string. We will create a testshow() method in which we will add the expected exception ArithmeticException. In this method, we will call the show() method of the ShowMessage class by using its object. We will create another test method, testfinalMessage(), in which we will concatenate the "Hello!" with the default message. In this message, we will check the actual result with the expected result using the assertEquals() method. The code of the above theory is given below: TestShowMessage.java Create TestRunner Class to Execute the Test CaseNow, we need to create a Java class TestRunner by using which we will execute our test cases. This class contains the main() method in which we run the testShowMessage class using the runClasses() method of the JUnitCore. In this method, we pass the class file of the code that contains the test cases. The result returned by the runClasses() method will store into the result variable of type Result. The result contains the failure and successful result both. There can be more than one failure, and to print each failure, we can iterate the result using the loop. The code of the above theory is given below: TestRunner.java Output: In order to run the test cases in Java, we compile all the Java classes associated with it using the javac command. After that, we run only that class which contains the main method, i.e., TestRunner.java. When we run the TestRunner.java file, it will give result true like: ![]() So, in order to write the test cases for the exception class, we need to add the exception class to that test case by using the expected keyword. |
In this section, we will learn how to create a mini-application for a banking system in Java. In this program, we will add some basic functionalities of a bank account like a deposit of amount, withdrawal of amount, etc. Initially, the program accepts the number of customers...
10 min read
In the world of Java programming, event-driven applications often rely on various types of events to handle user input, respond to system events, or perform other crucial tasks. Java provides a comprehensive event-handling framework that includes interfaces, classes, and methods to manage events effectively. One such...
5 min read
JSON is a very light weighted data interchange format that stores data in a key-value pair. In this section, we understand how we can convert JSON data to XML or XML data to JSON. Many times, we can come across a situation where we need to convert...
3 min read
In computer programming, a queue is a fundamental data structure that stores items in a linear order, adhering to the First-In-First-Out (FIFO) principle. It implies that the first element to be eliminated will be the one that was added first. Applications like work scheduling, event management,...
8 min read
The birthday paradox, or dilemma, is a concept in probability theory. Although this does not constitute a paradox in the sense that it results in a logical contradiction, it is referred regarded as such since the mathematical reality goes against common sense: most people believe that...
5 min read
Form feed character is one of the escape sequence characters it is denoted by "\f". It is an old strategy and used to show a page break. Example // java program for form feed Import java.io.*; class HelloWorld { public static void main(String[] args) { ...
4 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. To solve the problem, one wants to check the logical ability, critical thinking, and problem-solving skills of the interviewee. So, in this section, we are going to solve...
5 min read
In this section, we will understand what is Xmx in Java, how to set up maximum heap size for the Java application. In Java, sometimes when we run the Java application it shows an error message like the following: Error occurred during initialization of VM. Could not reserve...
3 min read
In this tutorial, we will be discussing Sparse numbers in Java. A sparse number is a number whose binary representation does not contain any two or more than two continuous set bits. Let's understand it with the help of a few examples. Example 1: Input int n =...
4 min read
In Java, array is the most important data structure that contains elements of the same type. It stores elements in contiguous memory allocation. There are two types of array i.e. static array and dynamic array. In this section, we will focus only on static array in...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India