mirror of
https://github.com/docker/docs.git
synced 2026-03-27 22:38:54 +07:00
## Description Migrate 17 Testcontainers guides from testcontainers.com into the Docker docs site, covering Java (14 guides), .NET (2 guides), and Node.js (1 guide). This follows up on PR #24450 which added the initial Go and Python guides. Each guide is converted from AsciiDoc to Hugo Markdown, split into multi-chapter stepper navigation, updated to the latest Testcontainers API, and verified with passing tests running in containers. Java guides use testcontainers-java 2.0.4 with the new 2.x Maven coordinates and package names (e.g., `testcontainers-postgresql`, `org.testcontainers.postgresql.PostgreSQLContainer`). The Quarkus guide uses Quarkus 3.22.3 with TC 1.x managed by the Quarkus BOM, since no released Quarkus version ships TC 2.x yet. ## How to test All code snippets have been verified by running each guide's source repository tests inside Docker containers with the Docker socket mounted. To re-run the verification, use the `/testcontainers-guides-migrator` skill included in this PR (`.claude/skills/testcontainers-guides-migrator/SKILL.md`). The skill's Step 6 documents the exact container commands and macOS Docker Desktop workarounds (host override, docker-java API version, etc.) needed to run each language's tests: ``` /testcontainers-guides-migrator I want you to verify all the guides in this branch. Do a full review, verifying that all code snippets compile, the code is executable, and ALL the tests pass. Run them as docker containers, never locally. ``` ## Related issues or tickets Supersedes #24450 (expanded from 2 guides to all 19) ## Reviews - [ ] Technical review - [ ] Editorial review - [ ] Product review --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
182 lines
4.1 KiB
Markdown
182 lines
4.1 KiB
Markdown
---
|
|
title: Create the Spring Boot project
|
|
linkTitle: Create the project
|
|
description: Set up a Spring Boot project with Spring Data JPA, PostgreSQL, and a REST API.
|
|
weight: 10
|
|
---
|
|
|
|
## Set up the project
|
|
|
|
Create a Spring Boot project from [Spring Initializr](https://start.spring.io)
|
|
by selecting the **Spring Web**, **Spring Data JPA**, **PostgreSQL Driver**, and
|
|
**Testcontainers** starters.
|
|
|
|
Alternatively, clone the
|
|
[guide repository](https://github.com/testcontainers/tc-guide-testing-spring-boot-rest-api).
|
|
|
|
The key dependencies in `pom.xml` are:
|
|
|
|
```xml
|
|
<properties>
|
|
<java.version>17</java.version>
|
|
<testcontainers.version>2.0.4</testcontainers.version>
|
|
</properties>
|
|
<dependencies>
|
|
<dependency>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
|
</dependency>
|
|
<dependency>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-starter-web</artifactId>
|
|
</dependency>
|
|
<dependency>
|
|
<groupId>org.postgresql</groupId>
|
|
<artifactId>postgresql</artifactId>
|
|
<scope>runtime</scope>
|
|
</dependency>
|
|
<dependency>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-starter-test</artifactId>
|
|
<scope>test</scope>
|
|
</dependency>
|
|
<dependency>
|
|
<groupId>org.testcontainers</groupId>
|
|
<artifactId>testcontainers-junit-jupiter</artifactId>
|
|
<scope>test</scope>
|
|
</dependency>
|
|
<dependency>
|
|
<groupId>org.testcontainers</groupId>
|
|
<artifactId>testcontainers-postgresql</artifactId>
|
|
<scope>test</scope>
|
|
</dependency>
|
|
<dependency>
|
|
<groupId>io.rest-assured</groupId>
|
|
<artifactId>rest-assured</artifactId>
|
|
<scope>test</scope>
|
|
</dependency>
|
|
</dependencies>
|
|
```
|
|
|
|
Using the Testcontainers BOM (Bill of Materials) is recommended so that you
|
|
don't have to repeat the version for every Testcontainers module dependency.
|
|
|
|
## Create the JPA entity
|
|
|
|
Create `Customer.java`:
|
|
|
|
```java
|
|
package com.testcontainers.demo;
|
|
|
|
import jakarta.persistence.Column;
|
|
import jakarta.persistence.Entity;
|
|
import jakarta.persistence.GeneratedValue;
|
|
import jakarta.persistence.GenerationType;
|
|
import jakarta.persistence.Id;
|
|
import jakarta.persistence.Table;
|
|
|
|
@Entity
|
|
@Table(name = "customers")
|
|
class Customer {
|
|
|
|
@Id
|
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
private Long id;
|
|
|
|
@Column(nullable = false)
|
|
private String name;
|
|
|
|
@Column(nullable = false, unique = true)
|
|
private String email;
|
|
|
|
public Customer() {}
|
|
|
|
public Customer(Long id, String name, String email) {
|
|
this.id = id;
|
|
this.name = name;
|
|
this.email = email;
|
|
}
|
|
|
|
public Long getId() {
|
|
return id;
|
|
}
|
|
|
|
public void setId(Long id) {
|
|
this.id = id;
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
public void setName(String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
public String getEmail() {
|
|
return email;
|
|
}
|
|
|
|
public void setEmail(String email) {
|
|
this.email = email;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Create the Spring Data JPA repository
|
|
|
|
```java
|
|
package com.testcontainers.demo;
|
|
|
|
import org.springframework.data.jpa.repository.JpaRepository;
|
|
|
|
interface CustomerRepository extends JpaRepository<Customer, Long> {}
|
|
```
|
|
|
|
## Add the schema creation script
|
|
|
|
Create `src/main/resources/schema.sql`:
|
|
|
|
```sql
|
|
create table if not exists customers (
|
|
id bigserial not null,
|
|
name varchar not null,
|
|
email varchar not null,
|
|
primary key (id),
|
|
UNIQUE (email)
|
|
);
|
|
```
|
|
|
|
Enable schema initialization in `src/main/resources/application.properties`:
|
|
|
|
```properties
|
|
spring.sql.init.mode=always
|
|
```
|
|
|
|
## Create the REST API endpoint
|
|
|
|
Create `CustomerController.java`:
|
|
|
|
```java
|
|
package com.testcontainers.demo;
|
|
|
|
import java.util.List;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
@RestController
|
|
class CustomerController {
|
|
|
|
private final CustomerRepository repo;
|
|
|
|
CustomerController(CustomerRepository repo) {
|
|
this.repo = repo;
|
|
}
|
|
|
|
@GetMapping("/api/customers")
|
|
List<Customer> getAll() {
|
|
return repo.findAll();
|
|
}
|
|
}
|
|
```
|