Skip to content

Common updates from JUnit 4 to JUnit 5

Ninette Adhikari edited this page Sep 25, 2024 · 35 revisions

Tests

@Test remains the same. However instead of org.junit.Test you now use:

org.junit.jupiter.api.Test

Assertions

Update the import to org.junit.jupiter.api.Assertions

Note for any assertions with error messages, the message is moved as the second argument.

For example:

// JUnit 4
assertTrue("No messages returned", messages != null && messages.size() > 0);

// JUnit 5
assertTrue(messages != null && messages.size() > 0, "No messages returned");

Before

Import: import org.junit.jupiter.api.BeforeEach

Replace @Before with @BeforeEach

BeforeClass

Import: import org.junit.jupiter.api.BeforeAll

Replace @BeforeClass with @BeforeAll

After

Import: import org.junit.jupiter.api.AfterEach

Replace @After with @AfterEach

AfterClass

Import: import org.junit.jupiter.api.AfterAll

Replace @AfterClass with @AfterAll

Category

Remove @Category because JUnit 5 doesn't support categories like JUnit 4. Instead, it uses tags (@Tag), which you can adapt based on your requirements.

Import @Tag like so: import org.junit.jupiter.api.Tag;

Then use @Tag as follows:

For example: @Category(AsyncLoggers.class) is replaced with @Tag("AsyncLoggers")

RunWith

MockitoJUnitRunner

Add imports:

import org.mockito.junit.jupiter.MockitoExtension;

import org.junit.jupiter.api.extension.ExtendWith;

Replace @RunWith(MockitoJUnitRunner.class) with @ExtendWith(MockitoExtension.class)

Parameterized

  • Replace @RunWith(Parameterized.class) with @ParameterizedTest. Also remove @Test as @ParameterizedTest will cover that.
  • Instead of @Parameterized.Parameters, use @MethodSource to supply the test parameters
  • Remove the constructor, as JUnit 5 passes parameters directly to the test methods
  • The data() method now returns a Stream<>, which JUnit 5 uses for parameterized tests

Example

// JUnit 5
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;

public static Stream<String> data() {
    return Stream.of("logger-config/LoggerConfig/", "logger-config/AsyncLoggerConfig/");
}

@ParameterizedTest(name = "{0}")
@MethodSource("data")

// JUnit 4
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import com.google.common.collect.ImmutableList;
import java.util.List;

@RunWith(Parameterized.class)
@Parameterized.Parameters(name = "{0}")
public static List<String> data() throws IOException {
    return ImmutableList.of("logger-config/LoggerConfig/", "logger-config/AsyncLoggerConfig/");
}

@Test

AssertThat

Since JUnit 5 does not directly provide assertThat, we switch to using org.hamcrest.MatcherAssert.assertThat from the hamcrest library.

Replace org.junit.Assert.assertThat with org.hamcrest.MatcherAssert.assertThat

ClassRule

@ClassRule is deprecated in Junit5 and, unfortunately, the translation is not straight forward. It depends on the implementation. At the moment we have successfully migrated one of is usages in this PR

I'll explain this example:

  • Now the class has a new interface @LoggerContextSource, to which we pass the value of the config file (value = "log4j2-jdbc-dbcp2.xml") and the timeout (the timeout value is from LoggerContextRule.createShutdownTimeoutLoggerContextRule)
  • In the test function, we no longer need to get the appender as it is provided as a parameter of the function. @Named is to specify which appender’s name you want to fetch. Before we did it like final Appender appender = LCR.getAppender("databaseAppender");, but now (I think) this function is inside the interface @LoggerContextSource, so we only need to specify the appender as a parameter in the test function public void test(@Named("databaseAppender") final Appender appender) {

Please update this section when they request changes for the PR we'll do in their side, to reflect the end result they are looking for.

Clone this wiki locally