intellij idea - Spring Data neo4j JUnit4 setup -
i confess total newbie @ java way of doing things , totally lost trying simple unit test running.
i building data access library , want unit test it. using spring data neo4j 4.0.0.build-snapshot because need connect remote neo4j server in real world.
after battling errors day @ point have test class:
@runwith(springjunit4classrunner.class) @componentscan(basepackages = {"org.mystuff.data"}) @contextconfiguration(classes={neo4jtestconfiguration.class}) public class personrepositorytest { @autowired personrepository personrepository; protected graphdatabaseservice graphdb; @before public void setup() throws exception { graphdb = new testgraphdatabasefactory().newimpermanentdatabase(); } @after public void teardown() { graphdb.shutdown(); } @test public void testcreateperson() throws exception { assertnotnull(personrepository); person p = new person("test", "user"); personrepository.save(p); } }
neo4jtestconfiguration.java
@configuration @enableneo4jrepositories(basepackages = "org.mystuff.data") @enabletransactionmanagement public class neo4jtestconfiguration extends neo4jconfiguration { @bean public sessionfactory getsessionfactory() { return new sessionfactory("org.mystuff.data"); } @bean public neo4jserver neo4jserver() { // return here? want in-memory database return null; } @bean @scope(value = "session", proxymode = scopedproxymode.target_class) public session getsession() throws exception { return super.getsession(); } }
when running tests personrepository.save() throws , exception 'no scope registered scope "session"' don't know if need configuration class test class won't work without because spring needs @contextconfiguration , want di niceness spring provides (amongst other things).
how can tests work spring?
you can use inprocessserver
in-memory database:
@bean public neo4jserver neo4jserver() { return new inprocessserver(); }
omit session scope test isn't running in web container. example: https://github.com/neo4j-examples/sdn4-cineasts/blob/4.0-rc1/src/test/java/org/neo4j/cineasts/persistencecontext.java
this require dependencies described in question: spring data neo4j 4.0.0.m1 test configuration
in test class personrepositorytest
, don't need construct instance of database, tests run against same inprocessserver
. here's example: https://github.com/neo4j-examples/sdn4-cineasts/blob/4.0-rc1/src/test/java/org/neo4j/cineasts/domain/domaintest.java
Comments
Post a Comment