A sed script to aid migration from JUnit 4 to JUnit 5.
- Download junit4-to-5.sed:
$(command -v wget && printf %s -O- || command -v curl) \
https://raw.githubusercontent.com/tingstad/junit4to5/master/junit4-to-5.sed \
> junit4-to-5.sed- Run script on java test files:
find . -path '*/src/test/*.java' -exec sed -Ei.bak -f junit4-to-5.sed {} \; -exec rm {}.bak \;- Update your
pom.xmldependencies, see this example or the User Guide.
Finally, fix any remaining build failures manually (or create an issue for me :))
The script replaces:
| Old | New |
|---|---|
org.junit.Test |
org.junit.jupiter.api.Test |
org.junit.Assert |
org.junit.jupiter.api.Assertions |
@Before, @After |
@BeforeEach, @AfterEach |
@BeforeClass, @AfterClass |
@BeforeAll, @AfterAll |
@Ignore |
@Disabled |
@Category |
@Tag |
@RunWith |
@ExtendWith |
@Test(timeout=3000) |
@Timeout(3) |
@Test(expected=Ex.class) m(){ ... |
@Test m(){ assertThrows(Ex.class, () -> ... |
@RunWith(Parameterized.class), @Test |
@ParameterizedTest |
assertTrue/assertFalse("msg", val) |
assertTrue/assertFalse(val, "msg") |
assertNull/assertNotNull("msg", val) |
assertNull/assertNotNull(val, "msg") |
assertEquals(msg, expected, actual) |
assert(Not)Equals(expected, actual, msg) |
assertSame/NotSame(msg, expected, actual) |
assert(Not)Same(expected, actual, msg) |
assertArrayEquals(msg, expected, actual) |
assertArrayEquals(expected, actual, msg) |
- Should only make changes with a high probability of being correct
- Should not change unaffected lines and formatting
- Should make changes non-breaking (compiling and passing) when possible
- Should leave difficult problems to the humans and let the test fail to be easily detectable
- It is almost ubiquitous, so the script has few dependencies and is easy to install and run
- Does not re-format code (see Philosophy)
- Simple search and replace is easy to implement
- Fun challenge to do a bit more complex tasks
- Does not presume assertion library, leaving
assertThatto fail - Does not parse arguments on multiple lines to shift the "message" parameter position of assertTrue/False/Equals
- Probably many features still missing