在junit测试中没有发生@Autowired依赖注入

时间:2014-07-20 23:02:16

标签: java spring spring-boot

我正在使用Spring Boot来引导spring-data-neo4j应用程序。我的单元测试(没有注入依赖项)运行正常,但是当我尝试在@Autowired - 带注释的类上运行@Service依赖项的集成测试时,它在NoSuchBeanDefinitionException上失败了。

由于某些原因,单元测试似乎没有加载上下文,但是我用@SpringApplicationConfiguration(classes = AppConfig.class)注释了测试 - 我还需要在这里做些什么吗?

配置类

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "net.foo.bar")
@EnableNeo4jRepositories(basePackages = "net.foo.bar.repo")
public class AppConfig extends Neo4jConfiguration {

    public AppConfig() {
        setBasePackage("net.foo.bar");
    }

    @Bean(destroyMethod = "shutdown")
    @Scope(BeanDefinition.SCOPE_SINGLETON)
    public GraphDatabaseService graphDatabaseService() {
        return new GraphDatabaseFactory().newEmbeddedDatabase(filePath);
    }

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(AppConfig.class, args);
    }
}

服务类:

package net.foo.bar.core.service
@Service
public class PostService implements EntityService<PostDAO,Post> {

    @Autowired
    Neo4jTemplate template;
    //...
    //don't think anything else here is relevant
}

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AppConfig.class)
public class PostTests {
    @Autowired
    PostService postService;

    @Test
    public void someTest(){ 
        postService.doSomething();
        //...
    }

 }

堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'net.foo.bar.PostTests': ....
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [net.foo.bar.core.service.PostService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 31 common frames omitted

更新

作为一种解决方法,我尝试自动发送对ApplicationContext的引用,并通过调用getBeanOfType()方法中的setUp()来实例化我的服务,而不是直接自动装配我的服务:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Epublic.class)
public class PostTests {
    @Autowired
    ApplicationContext ctx;

    PostService service; 

    @Before
    public void setUp() {
        service = ctx.getBean("postServiceImpl", PostService.class);
    }
}

这是有效的,但我觉得我正在达到目标但却忽略了这一点......

1 个答案:

答案 0 :(得分:1)

basePackages您没有@ComponentScan。您只有NeojConfiguration

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = { "net.foo.bar" })
@EnableNeo4jRepositories(basePackages = "net.foo.bar.repo")
public class AppConfig extends Neo4jConfiguration {

    public AppConfig() {
        setBasePackage("net.foo.bar");
    }

    @Bean(destroyMethod = "shutdown")
    @Scope(BeanDefinition.SCOPE_SINGLETON)
    public GraphDatabaseService graphDatabaseService() {
        return new GraphDatabaseFactory().newEmbeddedDatabase(filePath);
    }

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(AppConfig.class, args);
    }
}