使用Spring Boot和嵌入式驱动程序测试Neo4j

玛丽安

问题

我使用Neo4j数据库构建应用程序。我喜欢使用Spring Boot的@DataNeo4jTest注释测试一些自定义的Cypher查询(另请参阅Spring Boot Test-Neo4j),但是我遇到以下任一问题:

  • 该测试尝试使用BOLT驱动程序连接到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>

我的main/resources/application.yml是:

spring:
    data:
        neo4j:
            uri: bolt://localhost
            username: <username>
            password: <password>

我的test/resources/application.yml是:

spring.data.neo4j.uri: file:///neo4j.db

如果没有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.

随着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

问题

  • 是否缺少任何依赖项?
  • 配置错误吗?
  • 是否有人使用Spring Boot注释链接到工作示例@DataNeo4jTest

任何建议都欢迎。

玛丽安

我已经找到解决问题的办法。好像BOLT驱动程序也被用作默认测试程序-鉴于Spring Data Neo4j(SDN)文档,这令人困惑。最后,pom.xmlGitHub项目movie-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>

我保留,test/resources/application.yml删除了这一行:

spring.data.neo4j.uri: file:///neo4j.db

现在,测试上下文从嵌入式驱动程序开始,并创建一个file:/C:/Users/Me/AppData/Local/Temp/neo4j.db6943517458205762238/很棒的临时数据库文件,如我可以为每种测试方法获得一个干净的数据库实例。

我希望这个答案对其他有相同问题的人有所帮助。如有必要,我很乐意提供更多详细信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用嵌入式驱动程序时,将过程部署到Neo4J

Spring Boot-无法确定数据库类型NONE的嵌入式数据库驱动程序类

在Java中使用Neo4j驱动程序的结果

Spring Boot:无法确定数据库类型NONE的嵌入式数据库驱动程序类

使用MongoDB和C#新驱动程序版本(2.0)更新集合中的嵌入式文档

如何使用Neo4j Reactivity驱动程序发布/订阅Meteor

使用Cypher在嵌入式Neo4J应用程序中创建节点

带有Neo4j 3.0的Spring Data Neo4j 4:如何将配置传递给嵌入式驱动程序?

使用neo4j JS驱动程序进行套接字挂断

使用Neo4j Java驱动程序进行密码查询的执行时间

如何使用neo4j浏览器通过螺栓连接到neo4j嵌入式实例?

使用MongoDB和C#新驱动程序版本(2.0)更新嵌入式文档属性

在Python Bolt驱动程序中使用Neo4j事务

使用Neo4j .Net驱动程序的结果

使用C#驱动程序读取Neo4j查询的结果

使用Python驱动程序在Neo4j中创建多个节点的最佳方法

使用Javascript连接到Neo4j(无驱动程序)

无法使用neo4j驱动程序从nodejs连接到neo4j

在Spring中为HighlyAvailableGraphDatabase配置Neo4j 1.9.3(嵌入式)

在GWT中使用嵌入式Neo4j时出现AccessControlException

嵌入式Neo4j:使用图算法过滤节点属性

我可以在嵌入式neo4j数据库上使用Spring Data Neo4j 4.0.0吗?

升级到Neo4j 2.2.5和Spring Data 4.0.0.RELEASE-嵌入式数据库neo4jServer

使用OGM驱动程序从Neo4j数据库加载数据时发生NullPointerException

Derby 仅使用嵌入式驱动程序运行

无法在 Spring Boot 中使用 Redis 确定数据库类型 NONE 的嵌入式数据库驱动程序类

如何在嵌入式neo4j中使用“apoc.util.md5”

如何使用反应式驱动程序连接到 Neo4J 嵌入式实例?

无处发现使用嵌入式数据库 DatabaseManagementServiceBuilder 进行的 neo4j 4.0 测试