我使用Neo4j数据库构建应用程序。我喜欢使用Spring Boot的@DataNeo4jTest
注释测试一些自定义Cypher查询(另请参阅Spring Boot Test - Neo4j),但我遇到以下任一问题:
我的依赖项是在Spring Data Neo4j Reference Documentation之后使用Maven管理的。 SDN文档的第10.3.1节解释了:
默认情况下,SDN将使用BOLT驱动程序连接到Neo4j,您无需将其声明为pom中的单独依赖项。如果要在生产应用程序中使用嵌入式或HTTP驱动程序,则还必须添加以下依赖项。 (如果您只想使用嵌入式驱动程序进行测试,则不需要依赖嵌入式驱动程序。有关详细信息,请参阅下面Testing部分。)
因此,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
我得到以下异常,我认为这是由使用BOLT驱动程序引起的:
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
@DataNeo4jTest
?欢迎提出任何建议。
答案 0 :(得分:3)
我找到了解决问题的方法。似乎BOLT驱动程序也被用作测试的默认设置 - 考虑到Spring Data Neo4j(SDN)文档,这令人困惑。最后,GitHub项目的pom.xml
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>
我保留了test/resources/application.yml
但删除了这一行:
spring.data.neo4j.uri: file:///neo4j.db
现在,测试上下文从嵌入式驱动程序开始,并创建一个临时数据库文件,如file:/C:/Users/Me/AppData/Local/Temp/neo4j.db6943517458205762238/
,这很棒。我可以为每个测试方法获得一个干净的数据库实例。
我希望这个答案可以帮助那些有同样问题的人。如有必要,我很乐意提供更多细节。
答案 1 :(得分:1)
@DataNeo4JTest
与Spring Boot 2.x
一起很好地工作。
示例测试:
@RunWith(SpringRunner.class)
@DataNeo4jTest
public class WidgetRepositoryTest {
@Autowired
private WidgetRepository repository;
private Widget widget;
@Before
public void setUp() {
widget = WidgetTestData.builder().build();
}
@Test
public void itShouldSaveAndRetrieve() {
final Widget saved = repository.save(widget);
assertThat(saved.getId()).isNotNull();
assertThat(saved.getName()).isEqualTo(widget.getName());
final Optional<Widget> found = repository.findById(saved.getId());
assertThat(found).hasValueSatisfying(w-> {
assertThat(w.getId()).isEqualTo(saved.getId());
assertThat(w.getName()).isEqualTo(saved.getName());
});
}
}
我的Maven POM中与Neo4J相关的依赖项:
<dependency>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-embedded-driver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>