我正在测试由neo4j数据库支持的域逻辑和域对象。大多数这些测试需要模拟neo4j GraphDatabaseService
,各种Nodes
和各种Relationships
。一些模拟方法返回这些模拟对象。例如,getReferenceNode()调用返回一个模拟的Node或getSingleRelationship()调用返回一个模拟的关系,其getEndNode()依次返回一个模拟的Node。
我很担心返回嘲讽的嘲讽数量。通常,不建议这样做。它肯定会使测试设置变得复杂并导致非常脆弱的测试,因为需要模拟很多层的neo4j功能。
在单元测试neo4j支持的域逻辑时,有没有办法避免这种情况?
答案 0 :(得分:2)
您可以尝试使用临时数据库 - 每次都创建/刷新的数据库。如果您需要采样数据,那么您可以:
答案 1 :(得分: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>
否则nonmanentGraphDatabase将无法使用。
最后我可以使用干净的图表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 () {
...
}
}
我发现设置比使用普通方式快得多。将所有内容保存在内存中似乎有所回报