-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbClientTest.java
More file actions
76 lines (65 loc) · 2.37 KB
/
DbClientTest.java
File metadata and controls
76 lines (65 loc) · 2.37 KB
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 com.example.testing.db;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class DbClientTest {
/**
* H@ in-memory DB for speedy tests against a basic SQL DB.
* Doesn't support everything your real DB does, so you may not be able to
* test everything.
*/
@Nested
class WithH2 {
@Test
void can_fetch_users() {
var initScriptPsth = getClass().getResource("/data.sql").getPath();
var dbClient = new DbClient(
"jdbc:h2:mem:testdb;INIT=RUNSCRIPT FROM '%s'".formatted(initScriptPsth),
"sa",
"password");
var users = dbClient.getUsers();
assertThat(users).isEqualTo(List.of(
new DbClient.User(1, "a"),
new DbClient.User(2, "b"),
new DbClient.User(3, "c")
));
}
}
/**
* Using a real database in docker to test our DB code.
* Slower and has some issues with certain CI setups, but useful if you need to
* test against the real thing.
*/
@Nested
@Tag("testcontainers")
class WithTestContainers {
private static JdbcDatabaseContainer postgreSQLContainer;
@BeforeAll
static void setup() {
postgreSQLContainer = new PostgreSQLContainer("postgres:9.6.8")
.withDatabaseName("testsdb")
.withUsername("sa")
.withPassword("password")
.withInitScript("data.sql");
postgreSQLContainer.start();
}
@Test
void can_fetch_users() {
var dbClient = new DbClient(
postgreSQLContainer.getJdbcUrl(),
postgreSQLContainer.getUsername(),
postgreSQLContainer.getPassword());
var users = dbClient.getUsers();
assertThat(users).isEqualTo(List.of(
new DbClient.User(1, "a"),
new DbClient.User(2, "b"),
new DbClient.User(3, "c")
));
}
}
}