java - @Autowired 依赖注入(inject)在 junit 测试中没有发生

标签 java spring spring-boot

我正在使用 Spring Boot 来引导 spring-data-neo4j 应用程序。我的单元测试(没有注入(inject)依赖项)运行良好,但是当我尝试使用 @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

更新:

作为一种解决方法,我没有直接 Autowiring 我的服务,而是尝试 Autowiring 对 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);
    }
}

这是有效的,但我觉得我已经达到目标,但没有捕获重点......

最佳答案

您没有 @ComponentScanbasePackages。您只有 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);
    }
}

关于java - @Autowired 依赖注入(inject)在 junit 测试中没有发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24855737/

相关文章:

java - spring-jdbc 与 spring-data-jdbc 以及它们支持什么

java - Spring contextloaderlistener 在 bean 创建中的作用

java - Spring Boot - 不支持内容类型 'application/x-www-form-urlencoded;charset=UTF-8'

spring-boot - 是否可以在 Spring Boot 中使用咖啡因为每个缓存设置不同的规范?

spring-boot - Spring Webflux 抛出 "block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-2"

java - Log4J 2配置: How to split log files by size and by day at the same time?

java - 将 Map<String,Object> 转换为 JSON 并返回 Map

java - @Value boolean 返回值 true 的 'invalid boolean value'

java - 如果使用 env 初始化失败,如何加载一些默认的 log4j 配置。具体配置?

Java SSL 套接字分层在现有套接字 SSLSocketFactory : does it require a host parameter?