Skip to content

Instantly share code, notes, and snippets.

@Zincfox
Created August 10, 2022 19:17
Show Gist options
  • Save Zincfox/ca13156f90b55fad41a6aeaa8b4e83d3 to your computer and use it in GitHub Desktop.
Save Zincfox/ca13156f90b55fad41a6aeaa8b4e83d3 to your computer and use it in GitHub Desktop.
zonkyio-edbst missing refresh in nested tests
zonky.test.database.type=postgres
zonky.test.database.postgres.client.properties.currentSchema = app
zonky.test.database.refresh=before_each_test_method
spring.datasource.url=jdbc:postgresql://localhost:54320/app?currentSchema=app
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=user
spring.datasource.password=pass
spring.flyway.url=jdbc:postgresql://localhost:54320/app
spring.flyway.user=user
spring.flyway.password=pass
spring.flyway.schemas=app
logging.group.zonky=io.zonky.test
logging.level.zonky=trace
plugins {
id 'org.springframework.boot' version '2.5.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'de.robolab'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.flywaydb:flyway-core")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("io.zonky.test:embedded-database-spring-test:2.1.1")
testImplementation("org.flywaydb.flyway-test-extensions:flyway-spring-test:7.0.0")
}
tasks.named('test') {
useJUnitPlatform()
}
package de.robolab;
import io.zonky.test.db.AutoConfigureEmbeddedDatabase;
import org.flywaydb.test.annotation.FlywayTest;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.Arrays;
import java.util.stream.IntStream;
public class NestedTestBaseExample {
@SpringBootTest
@FlywayTest
@Sql(statements = {"INSERT INTO app.demo_table (id) VALUES (1),(2),(3);"})
@AutoConfigureEmbeddedDatabase(refresh = AutoConfigureEmbeddedDatabase.RefreshMode.BEFORE_EACH_TEST_METHOD)
static class TestContainerHost {
private final DataSource hostSource;
public TestContainerHost(@Autowired DataSource hostSource) {
this.hostSource = hostSource;
}
@Nested
class TestContainerImpl extends TestContainerBase {
public TestContainerImpl() {
super(hostSource);
}
@Test
public void OutsideTest() throws SQLException{
SubTest.assertSelectResults(
hostSource,
1,
2,
3
);
}
}
}
static abstract class TestContainerBase {
private final DataSource dataSource;
public TestContainerBase(DataSource dataSource) {
this.dataSource = dataSource;
}
@Nested
@AutoConfigureEmbeddedDatabase(refresh = AutoConfigureEmbeddedDatabase.RefreshMode.BEFORE_EACH_TEST_METHOD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SubTest {
public static void assertSelectResults(DataSource dataSource, int... expected) throws SQLException {
IntStream.Builder builder = IntStream.builder();
try (Statement statement = dataSource.getConnection().createStatement()) {
Assertions.assertTrue(statement.execute("SELECT (id) FROM app.demo_table;"), "SELECT reported no results");
ResultSet resultSet = statement.getResultSet();
SQLWarning warnings = resultSet.getWarnings();
if (warnings != null) {
Assertions.fail(warnings);
}
while (resultSet.next()) {
builder.add(resultSet.getInt(1));
}
}
int[] results = builder.build().toArray();
Assertions.assertArrayEquals(expected, results, "Got unexpected results: " + Arrays.toString(results));
}
@Test
@Order(-5)
public void selectElementsBeforeAllInsertTests() throws SQLException {
assertSelectResults(dataSource, 1, 2, 3);
}
@RepeatedTest(4)
@Order(0)
public void insertElementTest(RepetitionInfo info) throws SQLException {
int insertValue = info.getCurrentRepetition() + 3;
try (Statement insertStatement = dataSource.getConnection().createStatement()) {
Assertions.assertFalse(insertStatement.execute(
"INSERT INTO app.demo_table VALUES (" + insertValue + ");"),
"INSERT reported results instead of update-count");
Assertions.assertEquals(1, insertStatement.getUpdateCount());
}
assertSelectResults(dataSource, 1, 2, 3, insertValue);
}
@Test
@Order(5)
public void selectResetElementsAfterAllInsertTests() throws SQLException {
assertSelectResults(dataSource, 1, 2, 3);
}
}
}
}
CREATE SCHEMA IF NOT EXISTS app;
CREATE TABLE IF NOT EXISTS app.demo_table (
id INT NOT NULL
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment