如何在每次测试执行后清理自动装配的对象
How to clean autowired object after each test execution
如何在每次测试执行后清除自动装配的对象。在我的示例中,TestClassSettings 对象没有得到干净的 属性,它使用先前的测试 class 值。这是示例:
TestClassSettings.java
@Component
public class TestClassSettings{
private String titleUnitCode = "99";
private String fte = "1";
private String testIndicator = "";
public String getTitleUnitCode() {
return titleUnitCode;
}
public void setTitleUnitCode(String titleUnitCode) {
this.titleUnitCode = titleUnitCode;
}
public String getFte() {
return fte;
}
public void setFte(String fte) {
this.fte = fte;
}
public String getTestIndicator() {
return testIndicator;
}
public void setTestIndicator(String testIndicator) {
this.testIndicator = testIndicator;
}
}
testClassSettings 实例在测试用例之间没有变得干净。
TestLeaveHourCal_bweh6.java
@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml"})
@TestInstance(Lifecycle.PER_CLASS)
public class TestLeaveHourCal_bweh6 {
@Autowired
private ApproveTimesheetService approveTimesheetService;
@Autowired
private ComparePayUpdates comparePayUpdates;
@Autowired
public TestClassSettings testClassSettings; /* variable access type needs public */;
@Autowired
@RegisterExtension
protected CreateTimesheetBeforeTestExecutionCallback beforeTestExecutionCallback; /* can not be private */
@BeforeAll
public void setup() throws Exception {
/* START SETTINGS */
testClassSettings.setTestIndicator("6");
testClassSettings.setTitleUnitCode("99");
testClassSettings.setFte("0.5");
/* END SETTINGS */
}
@Test
@Order(1)
@Tag("setBaselinePayUpdates")
public void setBaselinePayUpdates() throws Exception {
}
添加了@DirtiesContext(classMode = ClassMode.AFTER_CLASS) 来解决问题
如何在每次测试执行后清除自动装配的对象。在我的示例中,TestClassSettings 对象没有得到干净的 属性,它使用先前的测试 class 值。这是示例:
TestClassSettings.java
@Component
public class TestClassSettings{
private String titleUnitCode = "99";
private String fte = "1";
private String testIndicator = "";
public String getTitleUnitCode() {
return titleUnitCode;
}
public void setTitleUnitCode(String titleUnitCode) {
this.titleUnitCode = titleUnitCode;
}
public String getFte() {
return fte;
}
public void setFte(String fte) {
this.fte = fte;
}
public String getTestIndicator() {
return testIndicator;
}
public void setTestIndicator(String testIndicator) {
this.testIndicator = testIndicator;
}
}
testClassSettings 实例在测试用例之间没有变得干净。
TestLeaveHourCal_bweh6.java
@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml"})
@TestInstance(Lifecycle.PER_CLASS)
public class TestLeaveHourCal_bweh6 {
@Autowired
private ApproveTimesheetService approveTimesheetService;
@Autowired
private ComparePayUpdates comparePayUpdates;
@Autowired
public TestClassSettings testClassSettings; /* variable access type needs public */;
@Autowired
@RegisterExtension
protected CreateTimesheetBeforeTestExecutionCallback beforeTestExecutionCallback; /* can not be private */
@BeforeAll
public void setup() throws Exception {
/* START SETTINGS */
testClassSettings.setTestIndicator("6");
testClassSettings.setTitleUnitCode("99");
testClassSettings.setFte("0.5");
/* END SETTINGS */
}
@Test
@Order(1)
@Tag("setBaselinePayUpdates")
public void setBaselinePayUpdates() throws Exception {
}
添加了@DirtiesContext(classMode = ClassMode.AFTER_CLASS) 来解决问题