I'm building a Spring Application using a Neo4j database. I have some services, that implement basic database-functions like persisting a user or finding a user by his username. Since I implemented some restraints, for example that it's not possible to delete a not existing user, i want to test my service. My wish would be having a test that builds a temporary Neo4j graphdb like described in http://neo4j.com/docs/stable/tutorials-java-unit-testing.html. But additionally I want to autowire my UserService into the test, do some operations on the temporary database and destroy the temporary database in the end again. I'm expecting that I can solve this with TestConfigurations, but since I'm not really experienced with Spring or Neo4j, it's not that simple.
I have the following configuration
@Configuration
@EnableNeo4jRepositories(basePackages = "de.myapp")
@EnableTransactionManagement
public class UserTestConfiguration extends Neo4jConfiguration{
@Bean
public UserService userService() {
return new UserBean();
}
@Bean
public Neo4jServer neo4jServer() {
//This is probably wrong since i really want to connect to the impermanent db
return new RemoteServer("http://localhost:7474");
}
@Bean
public SessionFactory getSessionFactory() {
return new SessionFactory("de.myapp");
}
}
And the following test-class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TSUserTestConfiguration.class)
public class TSUserBeanTest {
private GraphDatabaseService graphDb;
@Autowired
private TSUserService userService;
@Before
public void prepareTestDatabase() {
graphDb = new TestGraphDatabaseFactory().newImpermanentDatabase();
}
@After
public void destroyTestDatabase() {
graphDb.shutdown();
}
@Test
public void createUserTest() {
TSUser user = new TSUser("TestUser", "TestEmail");
//This should create the user in the impermanent db
userService.persistUser(user);
//assert stuff here
}
}
However, I get a NullPointer Exception for the graphDB in the destroy and I'm not even sure if I'm on the right way. Does someone have an example for this scenario maybe? Even a better way to integration-test my service on a temporary db is welcome.
Thanks and regards Urr4