-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoUsingTemplatedDatabaseTester.kt
89 lines (77 loc) · 2.72 KB
/
DemoUsingTemplatedDatabaseTester.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
79
80
81
82
83
84
85
86
87
88
89
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.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.Nested
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,
DemoUsingTemplatedDatabaseTester.DemoTestConfiguration::class
]
)
@AutoConfigureDbUnit
class DemoUsingTemplatedDatabaseTester @Autowired constructor(
private val dbUnit: DatabaseTester,
private val repository: DemoJdbcRepository
) {
@BeforeEach
fun createTheTable() {
repository.createTable()
}
@Nested
inner class RepositoryShouldQuerySuccessfully {
@Test
fun `when using overrides`() {
dbUnit.givenDataSet(
DemoUsingTemplatedDatabaseTester::class.java,
File("/TemplatedDemo.xml", Cell("[ID]", 123), Cell("[NAME]", "Test"))
)
val result = repository.selectAll()
assertThat(result).hasSize(1)
assertThat(result[0].id).isEqualTo(123)
assertThat(result[0].name).isEqualTo("Test")
}
@Test
fun `when using null overrides`() {
dbUnit.givenDataSet(
DemoUsingTemplatedDatabaseTester::class.java,
File("/TemplatedDemo.xml", Cell("[ID]", 123), Cell("[NAME]", null))
)
val result = repository.selectAll()
assertThat(result).hasSize(1)
assertThat(result[0].id).isEqualTo(123)
assertThat(result[0].name).isEqualTo(null)
}
}
@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)
}
}