java - 用于测试服务的临时 Neo4j 数据库

标签 java junit neo4j spring-data-neo4j-4

我正在使用 Neo4j 数据库构建 Spring 应用程序。 我有一些服务,它们实现基本的数据库功能,例如保留用户或通过用户名查找用户。 由于我实现了一些限制,例如无法删除不存在的用户,因此我想测试我的服务。 我的愿望是进行一个构建临时 Neo4j graphdb 的测试,如 http://neo4j.com/docs/stable/tutorials-java-unit-testing.html 中所述。 。 但另外我想将我的 UserService 自动连接到测试中,对临时数据库执行一些操作,并最终再次销毁临时数据库。 我希望我可以用 TestConfigurations 解决这个问题,但由于我对 Spring 或 Neo4j 没有真正的经验,所以事情没那么简单。

我有以下配置

@Configuration
@EnableNeo4jRepositories(basePackages = "de.myapp")
@EnableTransactionManagement
public class UserTestConfiguration extends Neo4jConfiguration{

  @Bean
  public UserService userService() {
    return new UserBean();
  }

  @Bean
  public Neo4jServer neo4jServer() {
    //This is probably wrong since i really want to connect to the impermanent db
    return new RemoteServer("http://localhost:7474");
  }

  @Bean
  public SessionFactory getSessionFactory() {
    return new SessionFactory("de.myapp");
  }
}

以及以下测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TSUserTestConfiguration.class)
public class TSUserBeanTest {

  private GraphDatabaseService graphDb;

  @Autowired
  private TSUserService userService;

  @Before
  public void prepareTestDatabase() {
    graphDb = new TestGraphDatabaseFactory().newImpermanentDatabase();
  }

  @After
  public void destroyTestDatabase() {
    graphDb.shutdown();
  }

  @Test
  public void createUserTest() {
    TSUser user = new TSUser("TestUser", "TestEmail");
    //This should create the user in the impermanent db
    userService.persistUser(user);
    //assert stuff here
  }
}

但是,我在销毁过程中得到了 graphDB 的 NullPointer 异常,我什至不确定我是否走在正确的道路上。 有人可能有这种情况的例子吗? 欢迎在临时数据库上集成测试我的服务的更好方法。

感谢和问候 Urr4

最佳答案

看来您正在使用 SDN 4.x。 - 哪个版本?

SDN 4.0 发布

您将使用 InProcessServer 代替 RemoteServer,它会在可用的本地端口上内部启动 CommunityNeoServer 的新实例。它还可以管理关闭,因此非常适合并推荐用于测试。 在您的配置中,使用它代替 RemoteServer-

@ bean 公共(public) Neo4jServer neo4jServer() { 返回新的 InProcessServer(); }

所需的依赖项记录在 reference guide 中。 进一步阅读,测试部分在这个blog post .

SDN 4.1 M1

InProcessServer 不再可用,您需要使用嵌入式驱动程序来为您设置临时图形数据库。 临时嵌入式存储的 ogm.properties 应包含-

driver=org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver

所需的依赖项记录在 reference guide 中。 可以在 here 找到升级测试以使用 SDN 4.1 的示例。 .

关于java - 用于测试服务的临时 Neo4j 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35998000/

相关文章:

java - 初始 SessionFactory 创建失败。java.lang.NoClassDefFoundError : org/hiber nate/cfg/Configuration

java - 使用 Spring Boot 运行测试

java - 测试顺序会影响性能结果吗?

neo4j - 数据库/API 名称中的 "4J"和 "J"是什么意思

java - BaseEntity @Inheritance - 如何从父类(super class)继承字段

java - 如何正确使用 JUNG 布局变换?

macos - OS X 上 IntelliJ 上的 JUnit - 无法分配请求的地址

java - 使用 JSON 作为参数的非托管扩展

node.js - nodejs neo4j 不访问参数 req.params.x 但接受任何硬编码值

java - 从一个 GUI 窗口跳转到另一个 GUI