spring-testcontainer

spring-testcontainer provides integration between the Spring (Data JPA) testing framework and Testcontainer library to easily test your application with different containerized database instances.

Лицензия

Лицензия

MIT
Категории

Категории

Контейнер
Группа

Группа

me.paulbares
Идентификатор

Идентификатор

spring-testcontainer
Последняя версия

Последняя версия

1.0.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

spring-testcontainer
spring-testcontainer provides integration between the Spring (Data JPA) testing framework and Testcontainer library to easily test your application with different containerized database instances.
Ссылка на сайт

Ссылка на сайт

https://github.com/paulbares/spring-testcontainer/
Организация-разработчик

Организация-разработчик

https://paulbares.me/
Система контроля версий

Система контроля версий

https://github.com/paulbares/spring-testcontainer

Скачать spring-testcontainer

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/me.paulbares/spring-testcontainer/ -->
<dependency>
    <groupId>me.paulbares</groupId>
    <artifactId>spring-testcontainer</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/me.paulbares/spring-testcontainer/
implementation 'me.paulbares:spring-testcontainer:1.0.1'
// https://jarcasting.com/artifacts/me.paulbares/spring-testcontainer/
implementation ("me.paulbares:spring-testcontainer:1.0.1")
'me.paulbares:spring-testcontainer:jar:1.0.1'
<dependency org="me.paulbares" name="spring-testcontainer" rev="1.0.1">
  <artifact name="spring-testcontainer" type="jar" />
</dependency>
@Grapes(
@Grab(group='me.paulbares', module='spring-testcontainer', version='1.0.1')
)
libraryDependencies += "me.paulbares" % "spring-testcontainer" % "1.0.1"
[me.paulbares/spring-testcontainer "1.0.1"]

Зависимости

compile (5)

Идентификатор библиотеки Тип Версия
org.testcontainers : junit-jupiter jar 1.15.1
org.testcontainers : jdbc jar 1.15.1
org.springframework.data : spring-data-jpa jar
org.hibernate : hibernate-core jar 5.4.15.Final
org.springframework.boot : spring-boot-starter-test jar

test (8)

Идентификатор библиотеки Тип Версия
com.h2database : h2 jar 1.4.200
com.zaxxer : HikariCP jar 3.4.5
org.testcontainers : mssqlserver jar 1.15.1
org.testcontainers : postgresql jar 1.15.1
org.testcontainers : mysql jar 1.15.1
com.microsoft.sqlserver : mssql-jdbc jar 8.4.1.jre11
org.postgresql : postgresql jar 42.2.18
mysql : mysql-connector-java jar 8.0.22

Модули Проекта

Данный проект не имеет модулей.

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:

@Container
static MySQLContainer container = new MySQLContainer("mysql");

and another one that will setup the Spring context:

@AnnotationContextCustomizer
static ContextCustomizer customizer = new JdbcDbContainerContextCustomizer(container);

The lifecycle of the container is managed by the Testcontainer library itself.

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:

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>mysql</artifactId>
    <version>1.15.0</version>
    <scope>test</scope>
</dependency>

Driver dependency:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.22</version>
</dependency>

If you want to customize the properties datasource, you can extend and override JdbcDbContainerContextCustomizer#getProperties.

Example

@Transactional
@SpringContainerTestDatabase
class TestMySQL {

  @Container
  static MySQLContainer container = new MySQLContainer("mysql");

  @AnnotationContextCustomizer
  static ContextCustomizer customizer = new JdbcDbContainerContextCustomizer(container);

  @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:

<dependency>
    <groupId>me.paulbares</groupId>
    <artifactId>spring-testcontainer</artifactId>
    <version>1.0.1</version>
</dependency>

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. DON'T FORGET to add a ContextCustomizer to your class otherwise your datasource will be misconfigured and Spring will fall back to an embedded in-memory H2 database.

FAQ

  • How can I check my test is correctly setup? You can verify in your test the connection url with the datasource:
@Autowired
DataSource dataSource;
...
String url = this.dataSource.getConnection().getMetaData().getURL();  // Value is jdbc:mysql://localhost:33349/test for a mysql db.

Версии библиотеки

Версия
1.0.1