【问题标题】:How can I clear all test data between two unit testing method?如何清除两种单元测试方法之间的所有测试数据?
【发布时间】:2019-07-03 09:35:33
【问题描述】:

我想为Flowable写一些单元测试,但是不同的方法可能会有数据冲突。如何清除两种测试方法之间的测试数据?

我尝试了注解@DirtiesContext,但没有成功。

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Import(TestConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class FlowableTest {
    @Autowired
    private ProcessEngine processEngine;

    @Test
    public void test() {
        IdentityService identityService = processEngine.getIdentityService();
        Group defaultGroup = identityService.newGroup("default");
        defaultGroup.setName("233333");
        identityService.saveGroup(defaultGroup);
    }

    @Test
    public void test2() {
        IdentityService identityService = processEngine.getIdentityService();
        Group defaultGroup = identityService.newGroup("default");
        defaultGroup.setName("233333");
        identityService.saveGroup(defaultGroup);
        // do some thing
    }
}

这是错误信息。

Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PUBLIC.PRIMARY_KEY_6B ON PUBLIC.ACT_ID_GROUP(ID_) VALUES 1"; SQL statement:
insert into ACT_ID_GROUP (ID_, REV_, NAME_, TYPE_)
    values (
      ?,
      1, 
      ?,
      ?
    ) [23505-199]

然后我想在每个方法之前手动创建表,我找到了“flowable.h2.create.history.sql”和“flowable.h2.create.engine.sql” 在 "flowable-engine-6.4.1.jar" 中,但是当我重新运行这些单元测试时,我发现缺少很多列。

有什么简单的方法可以清除所有测试数据?

【问题讨论】:

    标签: java spring-boot flowable


    【解决方案1】:

    在 Flowable 代码库中完成测试的方式以及我在其他项目中执行的方式是手动删除每个测试中的数据。其背后的原因是您知道自己在测试中创建了哪些数据。

    例如,在您的特定测试中,您可以在 @After 方法中删除所有组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      相关资源
      最近更新 更多