我们的项目中有数千个使用 JUnit4 构建的单元测试。目前,我们正在迁移到 JUnit5。
但是,assertXY
语句的不同方法签名(从 JUnit 4 更改为 JUnit 5)如果我们以手动方式迁移它们,则需要大量的手动工作和/或时间。
问题:
- 是否有一种快速的方法来实现此迁移(JUnit 4 -> JUnit 5),例如。使用转换器工具或
sed
命令来交换assertXY
语句的参数?
JUnit4 中的示例
@Test
public void testCreatePersonCorrect() {
String name = UUID.randomUUID().toString();
String title = UUID.randomUUID().toString();
Person person = service.createPerson(title, name);
assertNotNull("could not create a person", person);
assertEquals("title did not equal for that person", title, person.getTitle());
assertEquals("name did not equal for that person", name, person.getName());
}
JUnit5 中的示例
@Test
public void testCreatePersonCorrect() {
String name = UUID.randomUUID().toString();
String title = UUID.randomUUID().toString();
Person person = service.createPerson(title, name);
assertNotNull(person, "could not create a person");
assertEquals(title, person.getTitle(), "title did not equal for that person");
assertEquals(name, person.getName(), "name did not equal for that person");
}
最佳答案
您可能会找到 JUnit Pioneer 的 convert-junit4-to-junit5工具很有用。
如果您使用 IntelliJ IDEA,它具有从 JUnit 4 迁移到 JUnit Jupiter 的内置支持。请参阅 Trisha Gee 的 IntelliJ IDEA. Migrating from JUnit 4 to JUnit 5 YouTube 上的视频为例。
否则,请使用https://stackoverflow.com/a/65936638/388980中建议的重构技巧。可能是一个可行的替代方案。
关于java - 快速将assertXY语句从JUnit4迁移到Junit5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65936196/