Skip to content

Module bootique-jdbc-demo-testcontainer #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Contains two examples of [bootique-jdbc](https://github.com/bootique/bootique-jd

* [jdbc-app-demo](https://github.com/bootique-examples/bootique-jdbc-demo/tree/master/jdbc-app-demo) - core JDBC API
* [jdbc-test-demo-inmemory-db](https://github.com/bootique-examples/bootique-jdbc-demo/tree/master/jdbc-test-demo-inmemory-db) - JUnit 5 tests using Derby in-memory DB
* TODO: testcontainers demo
* [jdbc-test-demo-testcontainer](https://github.com/bootique-examples/bootique-jdbc-demo/tree/master/jdbc-test-demo-testcontainer) - JUnit 5 tests using TestContainers with Docker

For the examples working with the older versions of Bootique check the code on the branches:

Expand Down
18 changes: 18 additions & 0 deletions jdbc-test-demo-testcontainers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# jdbc-test-demo-testcontainer

Provides example JUnit5 tests using Bootique JDBC test API.

## Prerequisites

* Java 11 or newer
* Apache Maven
* Docker

## Run the demo tests

You need to have running Docker then you can run tests in bootique-jdbc-test demo module using TestContainer with Docker:
```bash
git clone [email protected]:bootique-examples/bootique-jdbc-demo.git
cd jdbc-test-demo-testcontainer
mvn clean test
```
72 changes: 72 additions & 0 deletions jdbc-test-demo-testcontainers/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.bootique.jdbc.demo</groupId>
<artifactId>jdbc-test-demo-testcontainers</artifactId>
<version>2.0.RC1</version>

<properties>
<maven.compiler.release>11</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<testcontainers.version>1.16.0</testcontainers.version>
<postgresql.version>42.3.1</postgresql.version>
<slf4j.version>1.7.25</slf4j.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.bootique.bom</groupId>
<artifactId>bootique-bom</artifactId>
<version>2.0.RC1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.bootique.jdbc</groupId>
<artifactId>bootique-jdbc-junit5-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.bootique.jdbc.test;

import io.bootique.BQRuntime;
import io.bootique.Bootique;
import io.bootique.jdbc.junit5.DbTester;
import io.bootique.jdbc.junit5.Table;
import io.bootique.jdbc.junit5.tc.TcDbTester;
import io.bootique.junit5.BQApp;
import io.bootique.junit5.BQTest;
import io.bootique.junit5.BQTestScope;
import io.bootique.junit5.BQTestTool;

@BQTest
public abstract class BasicTest {

// create a test DB
@BQTestTool(BQTestScope.GLOBAL)
static final DbTester db = TcDbTester
.db("jdbc:tc:postgresql:14:///mydb")

// make sure schema is created
.initDB("classpath:testcontainer-schema.sql")

// make sure test data is deleted before each test
.deleteBeforeEachTest("t1");

// create a global test app object, but do not run any commands
@BQApp(value = BQTestScope.GLOBAL, skipRun = true)
static final BQRuntime app = Bootique.app()
.autoLoadModules()

// make sure the test app is connected to our test DB
.module(db.moduleWithTestDataSource("db"))
.createRuntime();

protected Table t1() {
return db.getTable("t1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.bootique.jdbc.test;

import org.junit.jupiter.api.Test;

import java.sql.Types;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class InsertSelectUpdateTest extends BasicTest{

@Test
public void testUpdate() {

// implicit column names and positions
t1().insert(1, "x", "y");

// explicit column names
t1().insertColumns("c1", "c2", "c3")
.values(2, "a", "b")
.exec();

t1().update()
.set("c3", "c", Types.VARCHAR)
.where("c1", 2)
.exec();

// Note that "matcher" API is preferred for assertions, but sometimes you do need to select the data in a test
List<Object[]> data = t1().selectColumns("c3").select();
assertEquals(2, data.size());

assertArrayEquals(new Object[] {"y"}, data.get(0));
assertArrayEquals(new Object[] {"c"}, data.get(1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.bootique.jdbc.test;

import org.junit.jupiter.api.Test;

public class MatcherTest extends BasicTest{

@Test
public void testAssertMatches() {
t1().matcher().assertNoMatches();

t1().insert(1, "y", "z");
t1().matcher().assertOneMatch();

t1().insert(2, "a", "b");
t1().matcher().assertMatches(2);
}

@Test
public void testAssertMatches_Eq() {

t1().insertColumns("c1", "c2", "c3")
.values(1, "y", "z")
.values(2, "a", "b")
.exec();

t1().matcher().eq("c3", "z").eq("c1", 1).assertOneMatch();
}

@Test
public void testAssertMatches_Eq_Null() {

t1().insertColumns("c1", "c2", "c3")
.values(1, "y", "z")
.values(2, "a", null)
.exec();

t1().matcher().eq("c3", null).eq("c1", 2).assertOneMatch();
}

@Test
public void testAssertMatches_In() {

t1().insertColumns("c1", "c2", "c3")
.values(1, "y", "z")
.values(2, "a", "b")
.values(3, "c", "d")
.exec();

t1().matcher().in("c3", "z", "d").assertMatches(2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE "t1" ("c1" INT, "c2" VARCHAR(10), "c3" VARCHAR(10));