Let us take a look at how to test exceptions using Junit.
Calculation.javapackage org.code2care;
public class Calculation {
public int divide(int no1, int no2) {
return no1 / no2;
}
public void numberToString(int no) {
System.out.println("The no. entered is: " + Integer.toString(no));
}
}
CalculationClient.java
package org.code2care;
public class CalculationClient {
public static void main(String[] args) {
Calculation cal = new Calculation();
Integer no1 = 20;
Integer no2 = 10;
System.out.println(cal.divide(no1, no2));
cal.numberToString(no2);
}
}
We have two methods division that can throw ArithmeticException and numberToString that can throw a NullPointerException
Let's write test cases in Java JUnit to test these exceptions.
CalculationTest.javapackage myprog;
import org.code2care.Calculation;
import org.junit.Test;
/**
*
* JUnit test cases for
* testing exceptions
*
* @author code2care
*
*/
public class CalculationTest {
@Test(expected = ArithmeticException.class)
public void divideTwoNumbersArithmeticExceptionTest() {
Calculation calculation = new Calculation();
int no1 = 20;
int no2 = 0;
calculation.divide(no1, no2);
}
@Test(expected = NullPointerException.class)
public void noToStringNullPointerExceptionText() {
Calculation calculation = new Calculation();
Integer no1 = null;
calculation.numberToString(no1);
}
}

-
Have Questions? Post them here!
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- Indent/Prettify HTML File in Notepad++ - NotepadPlusPlus
- Fix SharePoint 2019 installation error This product requires Visual C++ Redistributable Package for Visual Studio 2017 - SharePoint
- Step-by-Step: Setting up Docker + Ubuntu Linux + Git + GitHub Tutorial - Git
- Fix Error 2711 SQL RBS client - The installer has encountered an unexpected error. The specified Feature name ('Docs') not found in Feature table - SharePoint
- MO229507 UK Europe users cannot access Microsoft 365 - SharePoint, OneDrive, Teams, Admin center, Planner, Yammer - Microsoft
- How to Reset Eclipse Theme to Classic - Eclipse
- [Android Studio] Hardcoded string Button, should use @string resource - Android-Studio
- [Java] Bad return type in lambda expression: int cannot be converted to boolean - Java