我有10个具有相同代码的测试用例。唯一的不同是数据。例如每个测试用例的beforeTestExecutionCallback.getCalData()值将有所不同。是否可以将模板中的测试方法代码提取出来,以便避免重复。我查看了@TestTemplate示例,但看起来参数是静态的。我有动态参数。老实说,由于我是Junit5(https://github.com/junit-team/junit5/blob/master/documentation/src/test/java/example/TestTemplateDemo.java)的新手,所以我也不了解该示例的工作方式。
@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml" })
@TestInstance(Lifecycle.PER_CLASS)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
public class MyTest {
@Autowired
protected ApproverProcess approve;
@Autowired
protected CompareProcess compare;
@Autowired
public TestClassSettings testClassSettings;
@Autowired
@RegisterExtension
TestClassBeforeTestExecutionCallback beforeTestExecutionCallback;
@BeforeAll
public void setup() throws Exception {
/* START SETTINGS */
testClassSettings.setFirstName("Joe");
testClassSettings.setLastName("Smith");
testClassSettings.setHours(40);
/* END SETTINGS */
}
@Test
public void approve() throws Exception {
approve.approveData(beforeTestExecutionCallback.getCalData(), true);
}
@Test
@DisplayName("Comapre")
public void compare() throws Exception {
approve.approveData(beforeTestExecutionCallback.getCalData(), false);
assertTrue(compare.compare(beforeTestExecutionCallback.getCalData(), beforeTestExecutionCallback.getTrackId(), beforeTestExecutionCallback.getId()), "Results are not matching");
}