私は現在、neo4j と春のデータを学習するためのいくつかのテスト サンプルを実装しています。cineast プロジェクトと spring-data hello-worlds のような他のサンプルに感謝します...
残念ながら、コードと他のサンプルコードのEclipseデバッガーにすでに時間を費やして、コンパイルを試みて解決策を見つけたとしても、理解できない問題に直面しています。この問題は、実際には些細なユースケースであっても、Neo4j の永続性を検証できないため、特にブロックされています...
ここに私がテストしているクラスがあります:
@NodeEntity
public class SimplePersonImpl {
@GraphId
private Long nodeId;
@Indexed
private String id = null;
@Indexed(indexType=IndexType.FULLTEXT, indexName = "LastName")
private String lastName = null;
@Indexed(indexType=IndexType.FULLTEXT, indexName = "FirstName")
private String firstName = null;
public SimplePersonImpl() {
}
public SimplePersonImpl(String firstName_, String lastName_) {
this.firstName=firstName_;
this.lastName=lastName_;
this.id=this.firstName+"."+this.lastName;
}
public String getID() {
return this.id;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public void setID(String id_) {
this.id=id_;
}
public void setFirstName(String firstName_) {
this.firstName=firstName_;
}
public void setLastName(String lastName_) {
this.lastName=lastName_;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SimplePersonImpl person = (SimplePersonImpl) o;
if (nodeId == null) return super.equals(o);
return nodeId.equals(person.nodeId);
}
@Override
public int hashCode() {
return nodeId != null ? nodeId.hashCode() : super.hashCode();
}
@Override
public String toString() {
return String.format("Person{first name='%s', last name='%s'}", firstName, lastName);
}
}
私は現在、次のjUnitテストケースに感謝してこのクラスをテストしています:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/yes-test-context.xml"})
@Transactional
public class SimplePersonImplTest {
@Autowired Neo4jTemplate template;
@Rollback(false)
@BeforeTransaction
public void cleanUpGraph() {
Neo4jHelper.cleanDb(template);
}
@Test @Transactional
public void persistedPersonShouldBeRetrievableFromGraphDB() {
SimplePersonImpl qqun = template.save(new SimplePersonImpl());
SimplePersonImpl retrievedQqun = template.findOne(qqun.getNodeId(), SimplePersonImpl.class);
assertEquals("retrieved person matches persisted one", qqun, retrievedQqun);
}
@Test @Transactional
public void persistedPersonWithPropertiesShouldBeRetrievableFromGraphDB() {
SimplePersonImpl qqun = template.save(new SimplePersonImpl("testFN","testLN"));
SimplePersonImpl retrievedQqun = template.findOne(qqun.getNodeId(), SimplePersonImpl.class);
assertEquals("check memory first name matches persisted one", qqun.getFirstName(), retrievedQqun.getFirstName());
assertEquals("check memory last name matches persisted one", qqun.getLastName(), retrievedQqun.getLastName());
}
}
最初のテストは正常に実行されますが、2 番目のテストは失敗します。Eclipseデバッガーで確認すると、次のことがわかります。
- save メソッドの後、正しい firtName、lastName、および id 値を持つ SimplePersonImpl qqun が返されます。しかし、nodeId が null で、その理由がわかりません。しかし、この動作は spring-data hello-worlds サンプルでも同じであるため、私の問題はそこから来ていないと思います。
- 私のqqunオブジェクトでnodeIdがnullであっても、findOneメソッドでは、qqun.getNodeId()は1を返します。この値がどこから来ているのかわかりませんが、続けましょう
- findOne メソッドは retrieveQqun を返します。検索されたすべての Qqun プロパティが null であり、firstName での最初の assertEquals が失敗した理由のようです。
ここで、私がどこで何か間違ったことをしたのか本当にわかりません (私がしたと思います)。これらの点についていくつかの回答をいただければ幸いです (保存呼び出しの後に nodeId=null が表示されるのはなぜですか? すべてのプロパティが null である非 null オブジェクトを取得するのはなぜですか? ...) .
いくつかのエラーが発生する可能性がある最後のポイントは、テスト コンテキストの構成ですが、ここでも問題がどこにあるのかわかりません。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="yes.ds.domain.neo4j"/>
<neo4j:config graphDatabaseService="graphDatabaseService"/>
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>
<!-- <neo4j:config storeDirectory="target/neo4j-db"/> -->
<neo4j:repositories base-package="yes.ds.repository.neo4j"/>
<tx:annotation-driven mode="proxy"/></beans>
ありがとうございました