我一直在努力为应用程序中构建的Neo4j存储库构建单元测试(运行良好)。目前,我只有一个类(UserRepositoryTest)和一个测试方法。我也有一个配置文件,使用嵌入式驱动程序在其中创建SessionFactory。
我得到的错误是:
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)
测试代码为:
@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());
}
}
}
配置代码为:
@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"));
}
任何帮助将不胜感激!
谢谢!