每次测试执行后如何清理自动装配的对象

时间:2019-04-22 19:33:46

标签: junit5

如何在每次测试执行后清洗自动装配的对象。在我的示例中,TestClassSettings对象没有clean属性,它使用以前的测试类值。这是示例:

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 {
    }

1 个答案:

答案 0 :(得分:0)

添加了@DirtiesContext(classMode = ClassMode.AFTER_CLASS)来解决此问题