# 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.
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.Assert;
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.");
}
}
🛠️ Auto-refactor Available
You can auto refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:
Need help? Check out our installation guide.
# Properties
Property | Value |
---|---|
Rule ID | ReplaceJUnit4AssertionsWithJupiter |
First seen in jSparrow version | 3.28.0 |
Minimum Java version | 8 |
Remediation cost | 2 min |