-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoUsingDatabaseTester.kt
78 lines (66 loc) · 2.33 KB
/
DemoUsingDatabaseTester.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package io.camassia.spring.dbunit
import io.camassia.spring.dbunit.api.DatabaseTester
import io.camassia.spring.dbunit.api.connection.DataSourceConnectionSupplier
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.jdbc.DataSourceBuilder
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.jdbc.core.JdbcTemplate
import java.math.BigInteger
import javax.sql.DataSource
@SpringBootTest(
classes = [
DemoJdbcRepository::class,
DemoUsingDatabaseTester.DemoTestConfiguration::class
]
)
@AutoConfigureDbUnit
class DemoUsingDatabaseTester @Autowired constructor(
private val dbUnit: DatabaseTester,
private val repository: DemoJdbcRepository
) {
@BeforeEach
fun createTheTable() {
repository.createTable()
}
@Test
fun `repository should insert successfully`() {
repository.insert(DemoJdbcRepository.DemoDao(123, "Test"))
val demoTable = dbUnit.createTable("demo")
assertThat(demoTable.rowCount).isOne
assertThat(demoTable.getValue(0, "id")).isEqualTo(BigInteger.valueOf(123))
assertThat(demoTable.getValue(0, "name")).isEqualTo("Test")
}
@Test
fun `repository should query successfully`() {
dbUnit.givenDataSet(DemoUsingDatabaseTester::class.java, "/Demo.xml")
val result = repository.selectAll()
assertThat(result).hasSize(1)
assertThat(result[0].id).isEqualTo(123)
assertThat(result[0].name).isEqualTo("Test")
}
@AfterEach
fun dropTheTable() {
repository.dropTable()
}
@TestConfiguration
class DemoTestConfiguration {
@Bean
fun dataSource(): DataSource = DataSourceBuilder
.create()
.driverClassName("org.h2.Driver")
.url("jdbc:h2:mem:dbunit")
.username("sa")
.password("sa")
.build()
@Bean
fun jdbc(ds: DataSource) = JdbcTemplate(ds)
@Bean
fun connectionSupplier(ds: DataSource) = DataSourceConnectionSupplier(ds)
}
}