简体   繁体   中英

@Autowired dependency injection not occurring in junit test

I'm using Spring Boot to bootstrap a spring-data-neo4j application. 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 .

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?

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:

@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 . You have only for 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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