Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-02-2019 05:04 AM
Hi,
I've been struggling with trying to build a unit test that will test a repository that I've 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", "moti.ben-yosef@company.com"));
users.add(createNewUserEntity(222223, "David Ben-Gurion", "david.ben-gurion", "david.ben-gurion@company.com"));
users.add(createNewUserEntity(222224, "Golan Eyal", "golan.eyal", "golan.eyal@company.com"));
users.add(createNewUserEntity(222225, "Moshe Haim", "moshe.haim", "moshe.haim@company.com"));
users.add(createNewUserEntity(222226, "Haim Yavin", "haim.yavin", "haim.yavin@company.com"));
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!
05-24-2019 07:22 PM
Same here. We have a half broken example I won't waste your time with, but we're forced to outsource this one, at this time. I'll post back if there are any generalized solutions we can share.
05-27-2019 07:29 AM
Hi,
I managed to get this stuff to work, here's my code. Hopefully this will help some lost souls out there
Test Class:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {TestConfiguration.class})
@DataNeo4jTest
public class UserRepositoryTest extends GraphDatabaseBuilder {
@Autowired
private UserRepository userRepository;
@Autowired
private SessionFactory sessionFactory;
List<UserEntity> users = new ArrayList<UserEntity>();
private Session session;
@Before
public void setUp() {
users = addUsers(userRepository);
session = sessionFactory.openSession();
}
@After
public void tearDown() {
session.clear();
}
public List<UserEntity> addUsers(UserRepository userRepository) {
List<UserEntity> users = new ArrayList<>();
users.add(createNewUserEntity(222222, "Moti Ben-Yosef", "moti.ben-yosef", "moti.ben-yosef@company.com", true, true));
users.add(createNewUserEntity(222223, "David Ben-Gurion", "david.ben-gurion", "david.ben-gurion@company.com", true, true));
users.add(createNewUserEntity(222224, "Golan Eyal", "golan.eyal", "golan.eyal@company.com",false, false));
users.add(createNewUserEntity(222225, "Moshe Haim", "moshe.haim", "moshe.haim@company.com", false, false));
users.add(createNewUserEntity(222226, "Haim Yavin", "haim.yavin", "haim.yavin@company.com", false, true));
for (UserEntity u : users) {
userRepository.save(u);
}
return users;
}
private UserEntity createNewUserEntity(long id, String name, String username, String email, boolean isManager, boolean active) {
UserEntity u = new UserEntity();
u.setId(id);
u.setName(name);
u.setUsername(username);
u.setEmail(email);
u.setManager(isManager);
if (active) u.activate();
if (!active) u.inActivate();
return u;
}
protected void verifyUser(UserEntity expectedUser, UserEntity returnedUser) {
Assert.assertEquals("User id mismatch", expectedUser.getId(), returnedUser.getId());
Assert.assertEquals("Name mismatch", expectedUser.getName(), returnedUser.getName());
Assert.assertEquals("User name mismatch", expectedUser.getUsername(), returnedUser.getUsername());
Assert.assertEquals("Email mismatch", expectedUser.getEmail(), returnedUser.getEmail());
Assert.assertEquals("isManager mismatch", expectedUser.isManager(), returnedUser.isManager());
}
/**
* Test of findById method, of class UserRepository.
*/
@Test
@Transactional(readOnly = true)
public void testFindUserById() {
for (UserEntity expectedUser : users) {
logger.info("testFindUserById: findById, id=" + expectedUser.getId());
Optional<UserEntity> result = userRepository.findById(expectedUser.getId());
Assert.assertNotNull("result = null", result);
Assert.assertTrue("No User returned", result.isPresent());
verifyUser(expectedUser, result.get());
}
}
}
Test Configuration Class:
@Configuration
@ComponentScan({"com.company.service.service"})
@EnableNeo4jRepositories(basePackages = "com.company.service.repository")
@EnableTransactionManagement
@AutoConfigurationPackage
public class TestConfiguration {
@Bean
public SessionFactory sessionFactory() {
EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService());
return new SessionFactory(driver, "com.company.service.domain");
}
@Bean
public PlatformTransactionManager transactionManager() {
return new Neo4jTransactionManager(sessionFactory());
}
@Bean
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(new File("test_graph.db"))
.newGraphDatabase();
}
}
User Repository:
public interface UserRepository extends Neo4jRepository<UserEntity, Long> {
@Query("match (u:User) where u.Id = {userId} return u")
Optional<UserEntity> findById(Long userId);
}
03-05-2020 09:41 PM
For what it's worth, GraphDatabaseFactory
has been renamed in Neo4j v4 to DatabaseManagementServiceBuilder
. I'm trying to get it to work like this:
val mgmtService = DatabaseManagementServiceBuilder(File("test_graph.db")).build()
mgmtService.createDatabase("default");
return mgmtService.database("default");
But I get...
org.neo4j.exceptions.DatabaseAdministrationException: Unsupported administration command: CREATE DATABASE `default`
All the sessions of the conference are now available online