A sed script to aid migration from JUnit 4 to 5.
find ./src/ -name \*.java -exec sed -E -i.bak -f junit4-to-5.sed {} \; -exec rm {}.bak \;
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(expected=Ex.class) m(){ ... |
@Test m(){ assertThrows(Ex.class, () -> ... |
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) |
Save a ton of work by employing a re-usable script instead of repeating manual chores.
Download and run file junit4-to-5.sed on all java test files:
$(command -v wget && printf %s -O- || command -v curl) \
https://raw.githubusercontent.com/tingstad/junit4to5/master/junit4-to-5.sed \
> junit4-to-5.sed
find ./src/test/ -name \*.java -exec sed -E -i.bak -f junit4-to-5.sed {} \; -exec rm {}.bak \;
Then update your pom.xml
dependencies, see this example or the User Guide.
Finally, fix any remaining build failures manually (or create an issue for me :))
- 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
assertThat
to fail - Does not parse arguments on multiple lines to shift the "message" parameter position of assertTrue/False/Equals
@Suite
not yet supported in JUnit 5- Probably many features still missing