Tutorials  /  JavaScript

JUnit Assert Exception - JUnit 5 and JUnit 4

Ccentron Redaktion · March 2024 ·2 min read ·JavaScript, 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.

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

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

JUnit 5 Assert Exception Message

Let’s say we have a class defined as:

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

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

Code
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.

Code
@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.

Go
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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren