无法连接到测试容器Neo4J实例? [英] Can't connect to a testcontainer Neo4J instance?
问题描述
这是我的测试课:
@Testcontainers
@ReactiveDataNeo4jTest
internal class RepositoryIT {
@Container
private val container = KNeo4jContainer.instance
@Test
fun `should answer with One`() {
val boltUrl = container.getBoltUrl()
GraphDatabase.driver(
boltUrl,
AuthTokens.basic("neo4j", "123456"))
.use { driver ->
driver.session().use { session ->
val res = session.run("OPTIONAL MATCH(n) RETURN 1 AS value")
val one = res.single().get("value").asInt()
assertThat(one).isEqualTo(2)
}
}
}
}
object KNeo4jContainer {
val instance by lazy {
startNeo4jContainer()
}
private fun startNeo4jContainer(): Neo4jContainer<*> =
Neo4jContainer<Nothing>("neo4j:4.1.0").apply {
withEnv("NEO4J_AUTH", "neo4j/123456")
withExposedPorts(7687)
withExposedPorts(7473)
withExposedPorts(7474)
start()
}
}
当我使用docker ps -a
进行检查时,似乎已经开始
When I check with docker ps -a
it seems to have started
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-------------- ----------------------------------- ------------------------ -------------------- ------------------- --------------------------------------------------------------------------- ----------------------------------------------------------
102b11fa5c7c neo4j:4.1.0 "/sbin/tini -g -- /d…" About a minute ago Up About a minute 0.0.0.0:32791->7473/tcp, 0.0.0.0:32790->7474/tcp, 0.0.0.0:32789->7687/tcp pensive_williams
92fac72190e0 testcontainersofficial/ryuk:0.3.0 "/app" About a minute ago Up About a minute 0.0.0.0:32788->8080/tcp testcontainers-ryuk-ecc3bfaf-44ba-482d-ae5f-ddba9a70182f
但是我无法连接到它:
org.neo4j.driver.exceptions.ServiceUnavailableException:无法执行 连接到localhost:7687,确保数据库正在运行,并且 到它的网络连接正常.
org.neo4j.driver.exceptions.ServiceUnavailableException: Unable to connect to localhost:7687, ensure the database is running and that there is a working network connection to it.
我可以通过docker-compose up
在具有相同配置的容器上运行测试:
I can run the test against a container started with the same configuration via docker-compose up
:
version: "3.1"
services:
neo4j:
image: neo4j:4.1.0
ports:
# Http
- "7474:7474"
# Https
- "7473:7473"
# Bolt
- "7687:7687"
environment:
# initial password reset
"NEO4J_AUTH": "neo4j/secret"
但是我认为这仅仅是因为在开发测试时不必启动新容器.
but I thought that would only be for not having to start a new container while developing tests.
我在做什么错了?
修改
在启动代码中将admin passwort设置为null之后,已将AuthTokens更改为AuthTokens.none()-仍然没有成功.
Changed AuthTokens to AuthTokens.none() after setting admin passwort null in the start-up code - still no success.
修改
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<!-- groupId, artifactId, version, name and description ommitted -->
<properties>
<java.version>11</java.version>
<kotlin.version>1.3.72</kotlin.version>
<neo4j.test.port>7687</neo4j.test.port>
<neo4j.test.auth>neo4j/secret</neo4j.test.auth>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor.kotlin</groupId>
<artifactId>reactor-kotlin-extensions</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-reactor</artifactId>
</dependency>
<dependency>
<groupId>org.neo4j.springframework.data</groupId>
<artifactId>spring-data-neo4j-rx-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency><dependency>
<groupId>org.neo4j.springframework.data</groupId>
<artifactId>spring-data-neo4j-rx-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.neo4j.springframework.data</groupId>
<artifactId>spring-data-neo4j-rx-spring-boot-test-autoconfigure</artifactId>
<version>1.1.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>neo4j</artifactId>
<version>1.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33.0</version>
<configuration>
<images>
<image>
<name>neo4j:4.1.0</name>
<alias>neo4j</alias>
<run>
<env>
<NEO4J_AUTH>${neo4j.test.auth}</NEO4J_AUTH>
</env>
<ports>
<port>${neo4j.test.port}:3306</port>
</ports>
<wait>
<!-- time based waiting is the only option, because TCP-based or log-based polling are not reliable.
see https://github.com/fabric8io/docker-maven-plugin/issues/328
Alternatively we can wait log- or tcp-based and try to connect for a while in the test code -->
<time>8000</time>
</wait>
</run>
</image>
</images>
</configuration>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/*IT.*</include>
</includes>
<systemPropertyVariables>
<neo4j.port>${neo4j.test.port}</neo4j.port>
<neo4j.auth>${neo4j.test.auth}</neo4j.auth>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<excludes>
<exclude>**/*IT.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
解决方案
private fun startNeo4jContainer(): Neo4jContainer<*> {
val container = Neo4jContainer<Nothing>("neo4j:4.1.0").apply {
withAdminPassword(null)
start()
}
return container
}
推荐答案
根据
从主持人的角度来看,Testcontainers实际上将其公开在
随机的免费端口.这是设计使然,以避免可能发生的端口冲突.
在本地运行的软件中或在并行测试运行之间出现.
From the host's perspective Testcontainers actually exposes this on a
random free port. This is by design, to avoid port collisions that may
arise with locally running software or in between parallel test runs. 以及> https://www.testcontainers.org/modules/databases的底部/jdbc/ 现在,在测试代码(或合适的设置方法)中,您可以获得
连接到该数据库所需的详细信息: Now, in your test code (or a suitable setup method), you can obtain
details necessary to connect to this database: 这篇关于无法连接到测试容器Neo4J实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
mysql.getJdbcUrl()
提供了您的代码可以连接到的JDBC URL mysql.getUsername()
提供您的代码应传递给驱动程序的用户名mysql.getPassword()
提供您的代码应传递给驱动程序的密码
mysql.getJdbcUrl()
provides a JDBC URL your code can connect tomysql.getUsername()
provides the username your code should pass to the drivermysql.getPassword()
provides the password your code should pass to the driver