oferbar
oferbar

Reputation: 176

Looking for a Neo4j example of a repository unit test with embedded driver that works

I've been struggling with building a unit test for a Neo4j repository that I built in the application (which is working fine). Currently I have a single class (UserRepositoryTest) with a single test method. I also have a configuration file where I create SessionFactory with the embedded driver.

The error I'm getting is this:

Caused by: java.lang.AbstractMethodError: Method org/neo4j/ogm/drivers/embedded/driver/EmbeddedDriver.newTransaction(Lorg/neo4j/ogm/transaction/Transaction$Type;Ljava/lang/Iterable;)Lorg/neo4j/ogm/transaction/Transaction; is abstract
  at org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver.newTransaction(EmbeddedDriver.java)
  at org.neo4j.ogm.session.transaction.DefaultTransactionManager.openTransaction(DefaultTransactionManager.java:68)
  at org.neo4j.ogm.session.Neo4jSession.beginTransaction(Neo4jSession.java:524)
  at org.springframework.data.neo4j.transaction.Neo4jTransactionManager.doBegin(Neo4jTransactionManager.java:179)

The test code is:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {TestConfiguration.class})
@DataNeo4jTest
@EnableTransactionManagement
public class UserRepositoryTest {


    @Autowired
    private UserRepository userRepository;

    @Autowired
    private SessionFactory sessionFactory;

    Session session;

    List<UserEntity> users = new ArrayList<UserEntity>();

    @Before
    public void setUp() {
        users.add(createNewUserEntity(222222, "Moti Ben-Yosef", "moti.ben-yosef", "[email protected]"));
        users.add(createNewUserEntity(222223, "David Ben-Gurion", "david.ben-gurion", "[email protected]"));
        users.add(createNewUserEntity(222224, "Golan Eyal", "golan.eyal", "[email protected]"));
        users.add(createNewUserEntity(222225, "Moshe Haim", "moshe.haim", "[email protected]"));
        users.add(createNewUserEntity(222226, "Haim Yavin", "haim.yavin", "[email protected]"));

        session = sessionFactory.openSession();

        session.query("match (u:User) where u.Id >= 222222 and u.Id <= 222226 detach delete(u)", new HashMap<String, Object>() );

        for (UserEntity u : users) {
            session.save(u);
        }
    }

    private UserEntity createNewUserEntity(long id, String name, String username, String email) {
        UserEntity u = new UserEntity();
        u.setUserId(id);
        u.setName(name);
        u.setUsername(username);
        u.setEmail(email);

        return u;
    }

    /**
     * Test of findByTitle method, of class UserRepository.
     */
    @Test
    public void testFindUserById() {
        for (UserEntity u : users) {
            Optional<UserEntity> result = userRepository.findById(u.getUserId());
            Assert.assertNotNull("result = null", result);
            Assert.assertTrue("No User returned", result.isPresent());
            UserEntity user = result.get();
            Assert.assertEquals("name mismatch", u.getName(), user.getName());
        }
    }
}

The configuration code is:

@Configuration
@EnableNeo4jRepositories(basePackages = "com.company.someservice.repository")
@EnableTransactionManagement
public class TestConfiguration {
@Bean
public SessionFactory sessionFactory() {
EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService());
return new SessionFactory(driver, "com.company.someservice.domain");
}

@Bean
public PlatformTransactionManager transactionManager() {
    return new Neo4jTransactionManager(sessionFactory());
}

@Bean
public GraphDatabaseService graphDatabaseService() {
    return new GraphDatabaseFactory().newEmbeddedDatabase(new File("test_graph.db"));
}

Any help would be greatly appreciated!

Thanks!

Upvotes: 0

Views: 239

Answers (1)

cybersam
cybersam

Reputation: 67044

Refer to this compatibility table, and make sure that you are using compatible libraries.

Upvotes: 1

Related Questions