Mar*_*ian 5 neo4j maven spring-data spring-boot spring-boot-test
我使用 Neo4j 数据库构建了一个应用程序。我喜欢使用 Spring Boot 的@DataNeo4jTest注释测试一些自定义 Cypher 查询(另请参阅Spring Boot Test - Neo4j),但我遇到了以下任一问题:
我的依赖项是按照Spring Data Neo4j 参考文档使用 Maven 管理的。SDN 文档的第 10.3.1 节解释了:
默认情况下,SDN 将使用 BOLT 驱动程序连接到 Neo4j,您无需在 pom 中将其声明为单独的依赖项。如果要在生产应用程序中使用嵌入式或 HTTP 驱动程序,还必须添加以下依赖项。(如果您只想使用嵌入式驱动程序进行测试,则不需要这种对嵌入式驱动程序的依赖。有关更多信息,请参阅下面的测试部分)。
因此,我的相关部分pom.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi=...>
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
...
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness</artifactId>
<version>3.3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
Run Code Online (Sandbox Code Playgroud)
我的main/resources/application.yml是:
spring:
data:
neo4j:
uri: bolt://localhost
username: <username>
password: <password>
Run Code Online (Sandbox Code Playgroud)
我的test/resources/application.yml是:
spring.data.neo4j.uri: file:///neo4j.db
Run Code Online (Sandbox Code Playgroud)
如果没有在test/resources/application.yml我出现以下情况例外,我以为是用螺栓驱动程序引起的:
org.springframework.transaction.CannotCreateTransactionException: Could not open Neo4j Session for transaction;
nested exception is org.neo4j.driver.v1.exceptions.AuthenticationException: The client is unauthorized due to authentication failure.
Run Code Online (Sandbox Code Playgroud)
随着在test/resources/application.yml我得到以下异常:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'neo4jAuditionBeanFactoryPostProcessor': Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method 'sessionFactory' threw exception;
nested exception is org.neo4j.ogm.exception.core.ConfigurationException: Could not load driver class org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver
Run Code Online (Sandbox Code Playgroud)
@DataNeo4jTest?欢迎任何建议。
我已经找到了解决我的问题的方法。似乎 BOLT 驱动程序也被用作测试的默认驱动程序 - 鉴于 Spring Data Neo4j (SDN) 文档,这令人困惑。最后,pom.xmlGitHub 项目movies-java-spring-data-neo4j 的帮助了我。我将以下测试依赖项添加到我的pom.xml:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-embedded-driver</artifactId>
<version>${neo4j-ogm.version}</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我保留了test/resources/application.yml但删除了该行:
spring.data.neo4j.uri: file:///neo4j.db
Run Code Online (Sandbox Code Playgroud)
现在,测试上下文从嵌入式驱动程序开始,并创建一个临时数据库文件,如file:/C:/Users/Me/AppData/Local/Temp/neo4j.db6943517458205762238/,这很棒。我可以为每个测试方法获得一个干净的数据库实例。
我希望这个答案能帮助其他有同样问题的人。如有必要,我很乐意提供更多详细信息。
| 归档时间: |
|
| 查看次数: |
3173 次 |
| 最近记录: |