-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoUsingTemplatedDatabaseTesterAndDefaults.kt
76 lines (65 loc) · 2.31 KB
/
DemoUsingTemplatedDatabaseTesterAndDefaults.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
package io.camassia.spring.dbunit
import io.camassia.spring.dbunit.api.DatabaseTester
import io.camassia.spring.dbunit.api.connection.DataSourceConnectionSupplier
import io.camassia.spring.dbunit.api.customization.TableDefaults
import io.camassia.spring.dbunit.api.dataset.Cell
import io.camassia.spring.dbunit.api.dataset.File
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 javax.sql.DataSource
@SpringBootTest(
classes = [
DemoJdbcRepository::class,
DemoUsingTemplatedDatabaseTesterAndDefaults.DemoTestConfiguration::class
]
)
@AutoConfigureDbUnit
class DemoUsingTemplatedDatabaseTesterAndDefaults @Autowired constructor(
private val dbUnit: DatabaseTester,
private val repository: DemoJdbcRepository
) {
@BeforeEach
fun createTheTable() {
repository.createTable()
}
@Test
fun `should query repository successfully`() {
dbUnit.givenDataSet(
DemoUsingTemplatedDatabaseTesterAndDefaults::class.java,
File("/TemplatedDemoWithDefaults.xml", Cell("[ID]", 123))
)
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)
@Bean
fun demoDefaults() = TableDefaults("demo", Cell("NAME", "Test"))
}
}