Tutorial

JUnit Assert Exception - JUnit 5 and JUnit 4

Published on August 3, 2022
author

Pankaj

JUnit Assert Exception - JUnit 5 and JUnit 4

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

We can test expected exceptions using JUnit 5 assertThrows assertion. This JUnit assertion method returns the thrown exception, so we can use it to assert exception message too.

JUnit Assert Exception

Here is a simple example showing how to assert exception in JUnit 5.

String str = null;
assertThrows(NullPointerException.class, () -> str.length());

JUnit 5 Assert Exception Message

Let’s say we have a class defined as:

class Foo {
	void foo() throws Exception {
		throw new Exception("Exception Message");
	}
}

Let’s see how we can test exception as well as its message.

Foo foo = new Foo();
Exception exception = assertThrows(Exception.class, () -> foo.foo());
assertEquals("Exception Message", exception.getMessage());

JUnit 4 Expected Exception

We can use JUnit 4 @Test annotation expected attribute to define the expected exception thrown by the test method.

@Test(expected = Exception.class)
public void test() throws Exception {
	Foo foo = new Foo();
	foo.foo();
}

JUnit 4 Assert Exception Message

If we want to test exception message, then we will have to use ExpectedException rule. Below is a complete example showing how to test exception as well as exception message.

package com.journaldev.junit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class JUnit4TestException {

	@Rule
	public ExpectedException thrown = ExpectedException.none();

	@Test
	public void test1() throws Exception {
		Foo foo = new Foo();
		thrown.expect(Exception.class);
		thrown.expectMessage("Exception Message");
		foo.foo();
	}
}

That’s all for a quick roundup on testing expected exceptions in JUnit 5 and JUnit 4.

You can check out more JUnit 5 examples from our GitHub Repository project.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
February 17, 2020

import org.junit.Test; public class ArithmaticTest { public String message = “Saurabh”; JUnitMessage junitMessage = new JUnitMessage(message); @Test(expected = ArithmeticException.class) public void testJUnitMessage(){ System.out.println("Junit Message is printing "); junitMessage.printMessage(); } @Test public void testJUnitHiMessage(){ message=“Hi!” + message; System.out.println("Junit Message is printing "); assertEquals(message, junitMessage.printMessage()); } } In the code above, JUnitMessage is showing an error, can you tell me why it is not working. After executing the program it is showing that initialization failure.

- sharath

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Featured on Community

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel