# Replace JUnit 4 Assertions with JUnit Jupiter

# Description

This rule contributes to a stepwise transition to JUnit 5 by replacing the JUnit 4 assertion methods by the equivalent JUnit 5 ones. In JUnit 5, all methods used to carry out assertions are declared in org.junit.jupiter.api.Assertions (opens new window), while in JUnit 4 the equivalent assertion methods are declared in org.junit.Assert (opens new window).

This rule looks for invocations of overloaded methods of the JUnit 4 class Assert, and replaces them with the invocations of equivalent - also overloaded - assertion alternatives declared in the JUnit 5 class Assertions. See the following table for the equivalent assertions.

Assertions in Junit 4 org.junit.Assert Assertions in JUnit 5 org.junit.jupiter.api.Assertions
Assert.assertArrayEquals (opens new window) Assertions.assertArrayEquals (opens new window)
Assert.assertEquals (opens new window) Assertions.assertEquals (opens new window)
Assert.assertFalse (opens new window) Assertions.assertFalse (opens new window)
Assert.assertNotEquals (opens new window) Assertions.assertNotEquals (opens new window)
Assert.assertNotNull (opens new window) Assertions.assertNotNull (opens new window)
Assert.assertNotSame (opens new window) Assertions.assertNotSame (opens new window)
Assert.assertNull (opens new window) Assertions.assertNull (opens new window)
Assert.assertSame (opens new window) Assertions.assertSame (opens new window)
Assert.assertTrue (opens new window) Assertions.assertTrue (opens new window)
Assert.fail (opens new window) Assertions.fail (opens new window)

By replacing each of these JUnit 4 Assert methods by the corresponding Jupiter alternatives, this rule promotes a stepwise transition to JUnit 5.

Requirements

This rule requires the following library to be present:

  • junit:junit:4.13
  • org.junit.jupiter:junit-jupiter-api:5.4.0

# Benefits

Migrates JUnit 4 tests to JUnit 5.

# Tags

# Code Changes

# Replacing Imports

Pre

import static org.junit.Assert.assertEquals;

import org.junit.jupiter.api.Test;

public class TestExample {
	@Test
	void test() {
		assertEquals(0L, 0L);
	}
}

Post

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class TestExample {
	@Test
	void test() {
		assertEquals(0L, 0L);
	}
}

# Replacing Invocations with a Message

Pre

import static org.junit.Assert.assertEquals;

import org.junit.jupiter.api.Test;

public class TestExample {
	@Test
	void test() {
		assertEquals("Expecting that 0L equals 0L.", 0L, 0L);
	}
}

Post

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class TestExample {
	@Test
	void test() {
		assertEquals(0L, 0L, "Expecting that 0L equals 0L.");
	}
}

# Replacing the Assert Qualifier

Pre

import static org.assertmethods.AssertMethods.assertEquals;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

public class TestExample {
	@Test
	void test() {
		Assert.assertEquals(0L, 0L);
		assertEquals(0L, 0L);
	}
}

Post

import static org.assertmethods.AssertMethods.assertEquals;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestExample {
	@Test
	void test() {
		Assertions.assertEquals(0L, 0L);
		assertEquals(0L, 0L);
	}
}

# Replacing ThrowingRunnable by Executable

Pre

import static org.junit.Assert.assertThrows;

import java.io.IOException;

import org.junit.function.ThrowingRunnable;
import org.junit.jupiter.api.Test;

public class ChangingTypeOfThrowingRunnable {
	@Test
	public void testExpectedIOException() {
		ThrowingRunnable runnable = () -> throwsIOException("Simply throw an IOException");
		assertThrows("Test changing type of ThrowingRunnable variable.", IOException.class, runnable);
	}
}

Post

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

public class ChangingTypeOfThrowingRunnable {
	@Test
	public void testExpectedIOException() {
		Executable runnable = () -> throwsIOException("Simply throw an IOException");
		assertThrows(IOException.class, runnable, "Test changing type of ThrowingRunnable variable.");
	}
}

You Want To Have Those Changes Done Automatically?

The automatic application of this rule is supported in the following jSparrow version:

# Properties

Property Value
Rule ID ReplaceJUnit4AssertionsWithJupiter
First seen in jSparrow version 3.28.0
Minimum Java version 8
Remediation cost 15 min