Dimitri Kopriwa
Dimitri Kopriwa

Reputation: 14363

Why can't I @Autowired my service directly in my test and why do I need to @Autowired the repository?

I have a spring boot application with Repository(s).

I also use @Service and extend the repository in it.

When I try to @Autowired the service I have:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.api.core.service.CountryService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

It work fine if I @Autowired the repository.

My test must look like this :

@SpringBootTest
@ActiveProfiles("core")
@ContextConfiguration(classes = { UserManagementConfig.class, UserManagementServiceConfig.class })
@UserManagementTx
@RunWith(SpringRunner.class)
public class BlogPostContentImplTest {
    @Autowired
    private CountryService countryService;
    @Autowired
    private BlogPostContentRepository blogPostContentRepository;
    private BlogPostContentServiceImpl blogPostContentService;
    private BlogPostContent entity = new BlogPostContent();
    @Before
    public void setUp() throws Exception {
        blogPostContentService = new BlogPostContentServiceImpl(blogPostContentRepository);
        List<Country> countryList = countryService.findAll(null);
        entity = new BlogPostContent();
        entity.setId(1L);
        entity.setDescription("test");
        entity.setCountry(countryList.get(0));
        blogPostContentService.insert(entity);
    }

    @After
    public void tearDown() throws Exception {
        blogPostContentService.delete(entity.getId());
    }

    @Test
    public void findAll() throws Exception {
        assertThat(blogPostContentService.findAll(null).size()).isGreaterThan(0);
    }

}

This is how I configure the context :

@Configuration
@ComponentScan({
        UserManagementConfig.CONTEXT_CLASSPATH
})
public class UserManagementConfig {
    public static final String CONTEXT_CLASSPATH = "com.api.userManagement.config.context.**";
}

@Configuration
@ComponentScan({
        UserManagementServiceConfig.CLASSPATH,
})
public class UserManagementServiceConfig {

    public static final String CLASSPATH = "com.api.userManagement.service.**";

}

Upvotes: 0

Views: 3007

Answers (1)

Pankaj Gadge
Pankaj Gadge

Reputation: 2814

Based on the code you posted, it seems you might need to component scan the com.api.core.service.CountryService Bean in one of your test context classes UserManagementConfig or UserManagementServiceConfig

@ComponentScan({
     basePackages = {"com.api.core.service"}
})

To test if app context loads your beans, you can create a TestContext class like below

@Configuration
@ComponentScan(basePackages = "com.api.core.service")
public class TestContext implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(TestContext.class, args);
    }
}

Have TestContext configured in your test like below

@ContextConfiguration(classes = {TestContext.class})

Upvotes: 1

Related Questions