vendredi 27 décembre 2019

JUnit tests not running in order

I am using JUnit 5 to test DynamoDB, and I have a set up method annotated with @BeforeAll where I insert 3 items in the database, and one annotated with @AfterAll to delete the items in the database after all the test have run.

@Test
@Order(1)
public void addNewCar() {
    repository.save(new Car("d"));

    assertThat(repository.count()).isEqualTo(4);
}

I am inserting a new item first, and then I delete one item:

@Test
@Order(2)
public void deleteCar() {
    repository.deleteById("a");

    assertThat(repository.count()).isEqualTo(2);
}

However, the first test is failing (the count is 3 instead of 4) because the car that is deleted in test number 2 is already removed in test number 1.

I am able to solve it by running by annotating the setup and clean database methods with @BeforeEach and AfterEach, but I am curious why my item is already deleted in test 1?

Aucun commentaire:

Enregistrer un commentaire