https://github.com/paulbares/spring-testcontainer
spring-testcontainer provides integration between the Spring (Data JPA) testing framework and Testcontainer library
https://github.com/paulbares/spring-testcontainer
container spring spring-boot spring-jpa testcontainers
Last synced: about 2 months ago
JSON representation
spring-testcontainer provides integration between the Spring (Data JPA) testing framework and Testcontainer library
- Host: GitHub
- URL: https://github.com/paulbares/spring-testcontainer
- Owner: paulbares
- Created: 2020-11-30T05:50:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-09-13T06:03:13.000Z (almost 3 years ago)
- Last Synced: 2025-02-17T23:05:16.095Z (over 1 year ago)
- Topics: container, spring, spring-boot, spring-jpa, testcontainers
- Language: Java
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Introduction
spring-testcontainer provides integration between the Spring (Data JPA) testing framework and Testcontainer library to
easily test your application with different containerized database instances.
# Configuration
The simplest way to start is to use the annotation `@SpringContainerTestDatabase` on your test class and add a static
container attribute that exposes a JDBC connection:
```java
@Container
static MySQLContainer container = new MySQLContainer("mysql");
```
The lifecycle of the container is managed by the Testcontainer library itself.
Edit or create the file `spring.factories` to add:
```
org.springframework.test.context.ContextCustomizerFactory=\
me.paulbares.InjectableContextCustomizerFactory
```
The `InjectableContextCustomizerFactory` is responsible to set the appropriate jdbc properties in the Spring context.
Don't forget to add as dependencies the Testcontainers library of the db you want to use and the associated JDBC driver library,
for instance for MySQL:
```xml
org.testcontainers
mysql
1.19.0
test
```
Driver dependency:
```xml
mysql
mysql-connector-java
8.0.33
```
If you want to customize the properties datasource, you can extend and override `JdbcDbContainerContextCustomizer#getProperties`.
# Example
```java
@Transactional
@SpringContainerTestDatabase
class TestMySQL {
@Container
static MySQLContainer container = new MySQLContainer("mysql");
@Autowired
CustomerRepository customerRepository;
@Test
void test() {
Customer c = new Customer();
c.setFirstname("admin");
c.setLastname("admin");
this.customerRepository.save(c);
Assertions.assertEquals(1, this.customerRepository.findAll().size()); // Whatever...
}
}
```
# Maven
To add a dependency on spring-testcontainer using Maven, use the following:
```xml
me.paulbares
spring-testcontainer
1.1.0
```
# Important
Per test class, only one field annotated with `@Container` is expected and this field must be static meaning only one docker
container will be started for all the tests within this class.
# FAQ
- How can I check my test is correctly setup?
You can verify in your test the connection url with the datasource:
```java
@Autowired
DataSource dataSource;
...
String url = this.dataSource.getConnection().getMetaData().getURL(); // Value is jdbc:mysql://localhost:33349/test for a mysql db.
```