4

I am writing tests for my application by class and have order method to do a specific scenario for each class, if I test each class separately it's work perfect but when I run mvn verfiy only the first-class success and the others filled because all of them use the same database when maven run.

how I can clean the database or drop and re-create it for each class? or if I can re-init the spring context for each class and it can re-create the database.

lib: SpirngBoot(2.3.0), de.flapdoodle.embed.mongo(2.2.0), Junit5(5.5.2)

Sample code of each class:

@SpringBootTest
@ExtendWith(SpringExtension::class)
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DefaultValuesTest {

    @Autowired
    private lateinit var testData: TestDataInit

    @Autowired
    private lateinit var usersRepo: UsersRepo

    @BeforeAll
    fun init() {
        testData.init()
    }

    @Test
    @Order(1)
    fun `check if init with default values`() {
        val users = usersRepo.findAll()
        Assertions.assertFalse(users.isNullOrEmpty())
    }
}

When every class start the method testData.init() the data exist before and some unique index throw errors.

Mohamd Ali
  • 2,146
  • 4
  • 23
  • 30

0 Answers0