临时Neo4j数据库来测试服务

时间:2016-03-14 21:01:52

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

我正在使用Neo4j数据库构建Spring应用程序。 我有一些服务,实现基本的数据库功能,如持久化用户或通过用户名查找用户。 由于我实施了一些约束,例如,无法删除不存在的用户,我想测试我的服务。 我的愿望是进行一次测试,构建一个像http://neo4j.com/docs/stable/tutorials-java-unit-testing.html中描述的临时Neo4j graphdb。 但另外我想将我的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
  }
}

但是,我在destroy中得到了一个N​​ullPointer Exception for graphDB,我甚至不确定我是否正确。 有人可能有这种情况的例子吗? 即使是在临时数据库上集成测试我的服务的更好方法也是受欢迎的。

谢谢和问候 Urr4

2 个答案:

答案 0 :(得分:3)

您似乎正在使用SDN 4.x. - 哪个版本?

SDN 4.0 RELEASE

而不是RemoteServer,您将使用InProcessServer在内部在可用的本地端口上启动CommunityNeoServer的新实例。它也将管理关闭,因此它是理想的并且建议用于测试。 在您的配置中,使用此代替RemoteServer -

@Bean   public Neo4jServer neo4jServer(){     返回新的InProcessServer();   }

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

SDN 4.1 M1

InProcessServer已不再可用,而您希望使用嵌入式驱动程序,它将为您设置Impermanent图数据库。 非永久嵌入式商店的ogm.properties应该包含 -

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

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

答案 1 :(得分:0)

我使用以下配置进行测试

@Configuration
@ComponentScan("my.services")
@EnableNeo4jRepositories("my.repos")
public class MyTestingConfiguration extends Neo4jConfiguration {

  @Override
  @Bean
  public Neo4jServer neo4jServer() {
    return new InProcessServer();
  }

  @Override
  public SessionFactory getSessionFactory() {
    return new SessionFactory("de.tsbros");
  }
}

以下用于测试的课程

@ContextConfiguration(classes = {MyTestingConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class TSUserBeanTest {
  @Autowired
  private MyService myService;

  //testing here
}

使用以下依赖项

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-neo4j</artifactId>
  <version>${sdn.version}</version>
  <type>test-jar</type>
</dependency>

<dependency>
  <groupId>org.neo4j</groupId>
  <artifactId>neo4j-kernel</artifactId>
  <version>${neo4j.version}</version>
  <type>test-jar</type>
</dependency>

<dependency>
  <groupId>org.neo4j.app</groupId>
  <artifactId>neo4j-server</artifactId>
  <version>${neo4j.version}</version>
  <type>test-jar</type>
</dependency>

<dependency>
  <groupId>org.neo4j</groupId>
  <artifactId>neo4j-ogm</artifactId>
  <version>${neo4j-ogm.version}</version>
  <type>test-jar</type>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.neo4j.test</groupId>
  <artifactId>neo4j-harness</artifactId>
  <version>${neo4j.version}</version>
  <scope>test</scope>
</dependency>

所描述的所有内容here 像魅力一样。