我相信我已经为 neo4j 测试容器启用了 apoc。
@Container
private static Neo4jContainer<?> neo4j = new Neo4jContainer<>(DockerImageName.parse("neo4j:4.2.14-enterprise"))
.withAdminPassword(password)
.withNeo4jConfig("dbms.security.procedures.unrestricted", "apoc.*")
.withEnv("NEO4JLABS_PLUGINS", "[\\\"apoc\\\"]")
.withEnv("NEO4J_ACCEPT_LICENSE_AGREEMENT", "yes")
.withReuse(true);
这是pom.xml
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</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>
但是,当我尝试运行单元测试时,出现以下错误:
org.neo4j.driver.exceptions.ClientException:没有为此数据库实例注册名称为
apoc.periodic.iterate
的过程。请确保您正确拼写了过程名称并且该过程已正确部署。
根据 Testcontainers Neo4j 模块文档,您可以使用特殊方法
withLabsPlugins()
代替您的 NEO4JLABS_PLUGINS
环境变量。喜欢:
...
.withLabsPlugins(Neo4jLabsPlugin.APOC)
...
这是基于 @yuppie-flu 已经发布的有用答案的更完整答案(这又基于此处的
testcontainers
文档:https://java.testcontainers.org/modules/databases/neo4j/ #add-neo4j-docker-labs-plugins)。
在
testcontainers
实例中启用 APOC 的最简洁示例是在新建 .withLabsPlugins(Neo4jLabsPlugin.APOC)
时包含 Neo4jContainer
,如下所示:
@Container
private static final Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>(imageName)
.withLabsPlugins(Neo4jLabsPlugin.APOC); // <-- this enables APOC
这是一个更完整的示例。此代码的一般用法是从任何测试中调用
TestGraph.getSession()
。这将启动一个新的 testcontainers 实例,或者重新使用当前 JVM 中已经运行的任何内容。
import org.neo4j.ogm.config.Configuration;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.testcontainers.containers.Neo4jContainer;
import org.testcontainers.containers.Neo4jLabsPlugin;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.utility.DockerImageName;
public class TestGraph {
@Container
private static final Neo4jContainer<?> neo4jContainer;
private static final SessionFactory sessionFactory;
static {
DockerImageName imageName = DockerImageName.parse("neo4j:5.9.0");
neo4jContainer = new Neo4jContainer<>(imageName)
.withLabsPlugins(Neo4jLabsPlugin.APOC) // <-- this enables APOC
.withoutAuthentication();
neo4jContainer.start();
String boltUrl = neo4jContainer.getBoltUrl();
Configuration configuration = new Configuration.Builder()
.uri(boltUrl)
.build();
sessionFactory = new SessionFactory(configuration, "your.package.here");
}
public static Session getSession() {
return sessionFactory.openSession();
}
}