我是由Neo4J数据库支持的单元测试域逻辑和域对象。这些测试大多数都需要模拟Neo4J GraphDatabaseService, , 各种各样的 Nodes, 和各种各样 Relationships. 。一些模拟的方法返回这些模拟对象。例如,getReferencenode()调用返回模拟的节点或getingLelationship()呼叫返回一个模拟的关系,其getendNode()又返回模拟的节点。

我担心返回模拟返回模拟的模拟数量。通常,不建议这样做。当然,它使测试设置变得复杂并导致了相当脆弱的测试,因为需要嘲笑这么多层的Neo4J功能。

在单位测试NEO4J支持的域逻辑时,有没有办法避免这种情况?

有帮助吗?

解决方案

您可以尝试使用临时数据库 - 每次都会创建/冲洗。如果您需要采样数据,则可以:

  1. 要么有一个固定装置,可以用数据填充新的DB。
  2. 具有每次运行测试时都使用的测试数据库设置(在这种情况下,您必须找出一种回滚更改或始终从已知状态开始的方法)

其他提示

我使用Maven,Spring Data Source和单元使用无症状GraphDatabase测试我的应用程序。由于很难将其设置在这里是我所做的:

在我的ApplicationContext.xml中,我初始化了GraphDatabaseservice:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    default-lazy-init="true">


    <neo4j:config graphDatabaseService="graphDatabaseService"/>
    <!--  use in memory graph database -->
    <bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase"/>

</beans>

在我的pom.xml中,我必须添加内核测试:

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-kernel</artifactId>
        <version>1.6</version>
        <classifier>tests</classifier>
        <scope>test</scope>
    </dependency>

否则,无症状GraphDatabase将无法使用。

最后,我可以使用干净的图DB evrytime:

public class MyNeo4JTest extends TestCase {

    protected ApplicationContext ctx;
    protected GraphDatabaseService gds;

    @Before
    public void setUp() throws Exception {

        // test-data
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        gds = ctx.getBean(GraphDatabaseService.class);
    }

    @Test
    public void testUser () {
          ...
    }
}

我发现该设置比使用普通方式快得多。保持一切都在记忆中似乎有回报

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top