Skip to content

Instantly share code, notes, and snippets.

@michael-simons
Created October 25, 2023 08:00
Show Gist options
  • Save michael-simons/9acdc43fc47095691412943b815f3caa to your computer and use it in GitHub Desktop.
Save michael-simons/9acdc43fc47095691412943b815f3caa to your computer and use it in GitHub Desktop.
An example how to run integration tests as GraalVM native image.
package demo;
import java.util.Map;
import java.util.stream.Collectors;
import org.neo4j.driver.Driver;
public record Movie(String id, String title) {
public static final class Repository {
private final Driver driver;
public Repository(Driver driver) {
this.driver = driver;
}
public Movie createOrUpdate(String title) {
return this.driver.executableQuery("MERGE (n:Movie {title: $title}) RETURN n")
.withParameters(Map.of("title", title))
.execute(Collectors.mapping(r -> {
var node = r.get("n").asNode();
return new Movie(node.elementId(), node.get("title").asString());
}, Collectors.toList()))
.stream()
.findFirst()
.orElseThrow();
}
}
}
<?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>org.example</groupId>
<artifactId>graalvm_native_integration_tests</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<junit-jupiter.version>5.10.0</junit-jupiter.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<maven-failsafe-plugin.version>3.2.1</maven-failsafe-plugin.version>
<maven.compiler.release>17</maven.compiler.release>
<native-maven-plugin.version>0.9.28</native-maven-plugin.version>
<neo4j-java-driver.version>5.13.0</neo4j-java-driver.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>2.0.9</slf4j.version>
<testcontainers.version>1.19.1</testcontainers.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit-jupiter.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-bom</artifactId>
<version>${slf4j.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>${neo4j-java-driver.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>neo4j</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<junit.platform.listeners.uid.tracking.enabled>true</junit.platform.listeners.uid.tracking.enabled>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native-maven-plugin.version}</version>
<configuration>
<metadataRepository>
<enabled>true</enabled>
</metadataRepository>
</configuration>
<executions>
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
package demo;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.testcontainers.containers.Neo4jContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers(disabledWithoutDocker = true)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RepositoryIT {
protected final Neo4jContainer<?> neo4j = new Neo4jContainer<>("neo4j:5.13.0")
.waitingFor(Neo4jContainer.WAIT_FOR_BOLT)
.withReuse(true);
protected Driver driver;
@BeforeAll
void startNeo4j() {
this.neo4j.start();
this.driver = GraphDatabase.driver(this.neo4j.getBoltUrl(),
AuthTokens.basic("neo4j", this.neo4j.getAdminPassword()));
}
@AfterAll
void closeDriver() {
if (this.driver == null) {
return;
}
this.driver.close();
}
@Test
void repositoryShouldWork() {
var repository = new Movie.Repository(driver);
var newMovie = repository.createOrUpdate("Event Horizon");
assertNotNull(newMovie.id());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment