简体   繁体   English

在junit测试中未发生@Autowired依赖项注入

[英]@Autowired dependency injection not occurring in junit test

I'm using Spring Boot to bootstrap a spring-data-neo4j application. 我正在使用Spring Boot引导spring-data-neo4j应用程序。 My unit tests (with no dependencies injected) run fine, but when I try to run an integration test with an @Autowired dependency on a @Service -annotated class, it's failing on a NoSuchBeanDefinitionException . 我的单元测试(没有注入依赖项)运行良好,但是当我尝试对@Service注释的类使用@Autowired依赖项运行集成测试时, NoSuchBeanDefinitionException失败了。

It seems like the context isn't being loaded in the unit test for some reason, but I've annotated the test with @SpringApplicationConfiguration(classes = AppConfig.class) - is there something else I need to do here? 似乎由于某种原因没有在单元测试中加载上下文,但是我已经用@SpringApplicationConfiguration(classes = AppConfig.class)注释了测试-在这里我还需要做其他事情吗?

Configuration 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);
    }
}

Service class: 服务等级:

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
}

Test class: 测试类别:

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

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

 }

Stack trace: 堆栈跟踪:

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

Update: 更新:

As a workaround, rather than autowiring my service directly, I tried autowiring a reference to the ApplicationContext and instantiating my service through a call to getBeanOfType() in my setUp() method: 作为一种解决方法,不是尝试直接自动装配服务,而是尝试自动装配对ApplicationContext的引用,并通过在setUp()方法中调用getBeanOfType()实例化我的服务:

@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);
    }
}

This is working, but I feel like I'm hitting the target but missing the point here... 这是可行的,但是我觉得我达到了目标,但是却错过了重点……

You don't have basePackages for @ComponentScan . 您没有@ComponentScan basePackages You have only for NeojConfiguration 您只有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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM