我想为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”中,但是当我重新运行这些单元测试时,我发现缺少很多列。
有什么简单的方法可以清除所有测试数据吗?
最佳答案
在 Flowable 代码库中完成测试的方式以及我在其他项目中执行的方式是手动删除每个测试中的数据。其背后的原因是您知道在测试中创建哪些数据。
例如,在您的特定测试中,您可以删除 @After
方法中的所有组。
关于java - 如何清除两种单元测试方法之间的所有测试数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867121/