1

I want to write some unit tests for Flowable, but different methods may have data conflicts. What should I do to clear the test data between two test methods?

I tried the annotation @DirtiesContext but it didn't work.

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

This is the error message.

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]

And then I want to create tables manually before each method, I found the "flowable.h2.create.history.sql" and "flowable.h2.create.engine.sql" in "flowable-engine-6.4.1.jar" ,but when I rerun these unit tests, I found there are lots of columns missing.

Is there any easy way to clear all test data?

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
wangbin
  • 21
  • 6

1 Answers1

0

The way the tests are done in the Flowable codebase and the way I am doing it in other projects is to manually delete the data within every test. The reason behind that is that you know which data you are creating in your tests.

For example in your particular test you can delete all groups in an @After method.

Filip
  • 19,269
  • 7
  • 51
  • 60