Skip to content

Commit d02de89

Browse files
authored
Merge pull request #16958 from etrandafir93/BAEL-8190-testcontainers_jdbc
BAEL-8190: testcontainers jdbc
2 parents 036c9f2 + 8714839 commit d02de89

File tree

6 files changed

+145
-5
lines changed

6 files changed

+145
-5
lines changed

spring-boot-modules/spring-boot-3-testcontainers/pom.xml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>spring-boot-3-testcontainers</artifactId>
77
<version>0.0.1-SNAPSHOT</version>
@@ -29,15 +29,26 @@
2929
<scope>runtime</scope>
3030
<optional>true</optional>
3131
</dependency>
32+
3233
<dependency>
3334
<groupId>org.springframework.boot</groupId>
34-
<artifactId>spring-boot-starter-test</artifactId>
35+
<artifactId>spring-boot-starter-data-jpa</artifactId>
3536
</dependency>
37+
<dependency>
38+
<groupId>org.postgresql</groupId>
39+
<artifactId>postgresql</artifactId>
40+
<version>${postgresql.version}</version>
41+
</dependency>
42+
3643
<dependency>
3744
<groupId>org.springframework.boot</groupId>
3845
<artifactId>spring-boot-starter-data-mongodb</artifactId>
3946
</dependency>
4047

48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-test</artifactId>
51+
</dependency>
4152
<dependency>
4253
<groupId>org.springframework.boot</groupId>
4354
<artifactId>spring-boot-testcontainers</artifactId>
@@ -49,6 +60,12 @@
4960
<version>${testcontainers.version}</version>
5061
<scope>test</scope>
5162
</dependency>
63+
<dependency>
64+
<groupId>org.testcontainers</groupId>
65+
<artifactId>postgresql</artifactId>
66+
<scope>test</scope>
67+
<version>${testcontainers.version}</version>
68+
</dependency>
5269
<dependency>
5370
<groupId>org.testcontainers</groupId>
5471
<artifactId>testcontainers</artifactId>
@@ -93,9 +110,10 @@
93110

94111
<properties>
95112
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
96-
<testcontainers.version>1.18.3</testcontainers.version>
113+
<testcontainers.version>1.19.8</testcontainers.version>
97114
<rest-assured.version>5.3.1</rest-assured.version>
98115
<java.version>17</java.version>
116+
<postgresql.version>42.7.3</postgresql.version>
99117
</properties>
100118

101-
</project>
119+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.testcontainers.jdbc;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
8+
@Entity
9+
public class Hobbit {
10+
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.IDENTITY)
13+
private Long id;
14+
15+
private String name;
16+
17+
public Hobbit(String name) {
18+
this.name = name;
19+
}
20+
21+
public Hobbit() {
22+
}
23+
24+
public Long getId() {
25+
return id;
26+
}
27+
28+
public void setId(Long id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.testcontainers.jdbc;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
public interface HobbitRepository extends CrudRepository<Hobbit, Long> {
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.testcontainers.jdbc;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
@SpringBootTest(properties =
10+
"spring.datasource.url= jdbc:tc:postgresql:16-alpine:///test-db"
11+
)
12+
class CustomTestcontainersDriverLiveTest {
13+
14+
@Autowired
15+
HobbitRepository theShire;
16+
17+
@Test
18+
void whenCallingSave_thenEntityIsPersistedToDb() {
19+
theShire.save(new Hobbit("Frodo Baggins"));
20+
21+
assertThat(theShire.findAll())
22+
.hasSize(1).first()
23+
.extracting(Hobbit::getName)
24+
.isEqualTo("Frodo Baggins");
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.testcontainers.jdbc;
2+
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.DynamicPropertyRegistry;
9+
import org.springframework.test.context.DynamicPropertySource;
10+
import org.testcontainers.containers.PostgreSQLContainer;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
@SpringBootTest
15+
class FullTestcontainersLifecycleLiveTest {
16+
17+
@Autowired
18+
HobbitRepository theShire;
19+
20+
static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:16-alpine").withDatabaseName("test-db");
21+
22+
@BeforeAll
23+
static void beforeAll() {
24+
postgres.start();
25+
}
26+
27+
@AfterAll
28+
static void afterAll() {
29+
postgres.stop();
30+
}
31+
32+
@DynamicPropertySource
33+
static void setProperties(DynamicPropertyRegistry registry) {
34+
registry.add("spring.datasource.url", postgres::getJdbcUrl);
35+
registry.add("spring.datasource.username", postgres::getUsername);
36+
registry.add("spring.datasource.password", postgres::getPassword);
37+
}
38+
39+
@Test
40+
void whenCallingSave_thenEntityIsPersistedToDb() {
41+
theShire.save(new Hobbit("Bilbo Baggins"));
42+
43+
assertThat(theShire.findAll())
44+
.hasSize(1).first()
45+
.extracting(Hobbit::getName)
46+
.isEqualTo("Bilbo Baggins");
47+
}
48+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.datasource.url: jdbc:tc:postgresql:16-alpine:///test-db
2+
spring.jpa.hibernate.ddl-auto: create

0 commit comments

Comments
 (0)