【问题标题】:Mocks returning mocks: Mocking neo4j (database) objects for unit testing of domain logic模拟返回模拟:模拟 Neo4j(数据库)对象以进行域逻辑的单元测试
【发布时间】:2011-11-30 05:06:07
【问题描述】:

我正在对 Neo4j 数据库支持的域逻辑和域对象进行单元测试。这些测试中的大多数需要模拟neo4j GraphDatabaseService、各种Nodes 和各种Relationships。一些模拟方法返回这些模拟对象。例如,getReferenceNode() 调用返回一个模拟节点,或者 getSingleRelationship() 调用返回一个模拟关系,其 getEndNode() 反过来又返回一个模拟节点。

我担心返回模拟的模拟数量返回模拟。通常,不建议这样做。它肯定会使测试设置复杂化并导致测试非常脆弱,因为需要模拟太多层的 neo4j 功能。

在对 Neo4j 支持的域逻辑进行单元测试时,有没有办法避免这种情况?

【问题讨论】:

    标签: java unit-testing neo4j


    【解决方案1】:

    您可以尝试使用临时数据库——每次都会创建/刷新。如果您需要对数据进行采样,那么您可以:

    1. 要么有一个用数据填充新数据库的夹具;
    2. 有一个测试数据库设置,每次运行测试时都会使用(在这种情况下,您必须想办法回滚更改或始终从已知状态开始)

    【讨论】:

    • 有一个现成的数据库可以为您执行此操作。使用 ImpermanentGraphDatabase,它是仅用于内存的,并且在您创建新数据库时始终是干净的。这就是我们(Neo 团队)用来测试我们自己的很多东西的方法。
    • 我使用ImpermanentGraphDatabase 进行集成/验收测试。也许这仍然是单元测试的方式。我将不得不查看该设置和拆卸的运行时成本。
    • 有没有办法从 Nodejs 访问它 - 只需通过 REST API?
    【解决方案2】:

    我正在使用 Maven、Spring 数据源并使用 ImpermanentGraphDatabase 对我的应用程序进行单元测试。 因为这里很难设置,所以我就是这样做的:

    在我的 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>
    

    否则 impermanentGraphDatabase 将不可用。

    我终于可以使用干净的图形数据库 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 () {
              ...
        }
    }
    

    我发现设置比使用正常方式快得多。将所有内容都保存在内存中似乎是有回报的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多