代码之家  ›  专栏  ›  技术社区  ›  SteveS

无法使用注释配置Neo4j和JUnit

  •  0
  • SteveS  · 技术社区  · 11 年前

    我正在将spring.io指南转换为 accessing Neo4j 使用JUnit。然而,这并不奏效。我收到消息:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hello.TestIt': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: requirement failed: Can't work with a null graph database
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]
    ....
    

    我向pom添加了两个依赖项:

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    

    这是完整的单元测试。这是main()的副本,其中包含我认为JUnit所必需的配置更改:

    package hello;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.neo4j.graphdb.GraphDatabaseService;
    import org.neo4j.graphdb.Transaction;
    import org.neo4j.graphdb.factory.GraphDatabaseFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
    import org.springframework.data.neo4j.config.Neo4jConfiguration;
    import org.springframework.data.neo4j.core.GraphDatabase;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.support.AnnotationConfigContextLoader;
    
    
    @EnableNeo4jRepositories(basePackages = "hello")
    @ContextConfiguration(loader=AnnotationConfigContextLoader.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class TestIt extends Neo4jConfiguration {
    
        @Configuration
        static class ContextConfiguration {
            @Bean
            GraphDatabaseService graphDatabaseService() {
                return new GraphDatabaseFactory().newEmbeddedDatabase("accessingdataneo4j.db");
            }
        }
    
        @Before
        public void init() {
            setBasePackage("hello");
        }
    
        @Autowired
        PersonRepository personRepository;
    
        @Autowired
        GraphDatabase graphDatabase;
    
        @Test
        public void t1() {
            Person greg = new Person("Greg");
            Person roy = new Person("Roy");
            Person craig = new Person("Craig");
    
            System.out.println("Before linking up with Neo4j...");
            for (Person person : new Person[]{greg, roy, craig}) {
                System.out.println(person);
            }
    
            Transaction tx = graphDatabase.beginTx();
            try {
                personRepository.save(greg);
                personRepository.save(roy);
                personRepository.save(craig);
    
                greg = personRepository.findByName(greg.name);
                greg.worksWith(roy);
                greg.worksWith(craig);
                personRepository.save(greg);
    
                roy = personRepository.findByName(roy.name);
                roy.worksWith(craig);
                // We already know that roy works with greg
                personRepository.save(roy);
    
                // We already know craig works with roy and greg
    
                System.out.println("Lookup each person by name...");
                for (String name: new String[]{greg.name, roy.name, craig.name}) {
                    System.out.println(personRepository.findByName(name));
                }
    
                System.out.println("Looking up who works with Greg...");
                for (Person person : personRepository.findByTeammatesName("Greg")) {
                    System.out.println(person.name + " works with Greg.");
                }
    
                tx.success();
            } finally {
                tx.close();
            }
        }
    }
    
    1 回复  |  直到 11 年前
        1
  •  1
  •   Michael Hunger    11 年前

    我从未见过将测试本身用作配置,尝试将配置移动到静态内部类并将其声明为依赖项。我经常使用这种模式,而且效果很好。

    我想,否则自我注射不起作用。