Skip to content

Commit

Permalink
fix: reset db before each test
Browse files Browse the repository at this point in the history
The row IDs are unpredictable on subsequent test runs due to the ID
sequence not getting reset. Add a DB fixture for force-resetting the
database state to a blank slate before each test, to ensure things are
in a predictable state.
  • Loading branch information
Kailari committed Sep 5, 2024
1 parent 709fe18 commit 73bb730
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/test/kotlin/fi/oph/kitu/KituApplicationTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package fi.oph.kitu

import fi.oph.kitu.oppija.Oppija
import fi.oph.kitu.oppija.OppijaService
import fi.oph.kitu.test.DBFixture
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import kotlin.test.assertContentEquals

@SpringBootTest
class KituKotlinApplicationTests {
class KituKotlinApplicationTests : DBFixture() {
@Autowired
private lateinit var oppijaService: OppijaService

Expand Down
15 changes: 15 additions & 0 deletions src/test/kotlin/fi/oph/kitu/test/DBFixture.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fi.oph.kitu.test

import org.junit.jupiter.api.BeforeEach
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.jdbc.core.JdbcTemplate

abstract class DBFixture {
@Autowired
private lateinit var jdbcTemplate: JdbcTemplate

@BeforeEach
fun nukeDB() {
jdbcTemplate.execute("TRUNCATE oppija RESTART IDENTITY ")
}
}

0 comments on commit 73bb730

Please sign in to comment.