-
Notifications
You must be signed in to change notification settings - Fork 0
Common updates from JUnit 4 to JUnit 5
@Test
remains the same. However instead of org.junit.Test
you now use:
org.junit.jupiter.api.Test
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");
Import: import org.junit.jupiter.api.BeforeEach
Replace @Before
with @BeforeEach
Import: import org.junit.jupiter.api.BeforeAll
Replace @BeforeClass
with @BeforeAll
Import: import org.junit.jupiter.api.AfterEach
Replace @After
with @AfterEach
Import: import org.junit.jupiter.api.AfterAll
Replace @AfterClass
with @AfterAll
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")
Add imports:
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.extension.ExtendWith;
Replace @RunWith(MockitoJUnitRunner.class)
with @ExtendWith(MockitoExtension.class)
@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 decorator
@LoggerContextSource
, to which we pass the value of the config file (value = "log4j2-jdbc-dbcp2.xml"
) and thetimeout
(the timeout value is fromLoggerContextRule.createShutdownTimeoutLoggerContextRule
) - In the test function, we no longer need to get the appender as it is provided as a parameter of the function.
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.