How autowire a repository in an integration test with Spring Boot?

Viewed 14307

I'm trying to write an integration test but I'm having trouble with autowiring the repository in the test.

I receive this exception: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.observer.media.repository.ArticleRepository]: Specified class is an interface.

Edit: I added PersistenceConfig.class with @EnableJpaRepositories, code below. This results in Exception org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

I also tried to add Application.class in @SpringBootTest(classes = {} in an catch all attempt but that throws Error creating bean with name 'articleRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class org.observer.media.model.Article

ScraperRunnerIntegrationTest (the config classes only contain beans of domain classes):

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {
        ApplicationConfig.class,
        PersistenceConfig.class,
        DeMorgenTestConfig.class,
        Article.class,
        ScraperRunner.class,
        DeMorgenWebsiteScraper.class,
        ArticleService.class,
        DeMorgenPageScraper.class,
        JsoupParser.class,
        DeMorgenArticleScraper.class,
        GenericArticleScraper.class,
        ImageMetaScraper.class,
        ArticlePrinter.class,
        ArticleRepository.class
})
public class ScraperRunnerIntegrationTest {

    private final static Article EXPECTED_ARTICLE_1 = anArticle().withHeadline("headline1").build();
    private final static Article EXPECTED_ARTICLE_2 = anArticle().withHeadline("headline2").build();

    @Autowired
    private ScraperRunner scraperRunner;
    @Autowired
    private DeMorgenWebsiteScraper deMorgenWebsiteScraper;

    @Autowired
    private ArticleRepository articleRepository;

    @Test
    public void run() {
        scraperRunner.run(deMorgenWebsiteScraper);

        assertThat(articleRepository.findAll()).containsOnly(EXPECTED_ARTICLE_1, EXPECTED_ARTICLE_2);
    }

Repository:

import org.observer.media.model.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ArticleRepository extends JpaRepository<Article, Long> {

    List<Article> findAll();

    Article findByHash(String hash);

    Article findByHeadline(String headline);

    List<Article> findArticleByHeadlineAndCompanyName(String headline, String companyName);

    @Query("SELECT CASE WHEN COUNT(a) > 0 THEN true ELSE false END FROM Article a WHERE a.hash = :hash")
    boolean existsByHash(@Param("hash") String hash);
}

PersistenceConfig.class:

package org.observer.media.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@EnableJpaRepositories("org.observer.media.repository") 
public class PersistenceConfig {
}
3 Answers

I had the same exception org.springframework.beans.BeanInstantiationException: Failed to instantiate [...]: Specified class is an interface on my integration test. I had resolved without using a PersistenceConfig.class but changed only the annotaion used in my integration class that was wrong. I have used the following annotation: @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) because I didn't want use a in-memory database.

Your code could be the following:

@ComponentScan(basePackages = {"org.observer"})
@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class ScraperRunnerIntegrationTest {

    private final static Article EXPECTED_ARTICLE_1 = anArticle().withHeadline("headline1").build();
    private final static Article EXPECTED_ARTICLE_2 = anArticle().withHeadline("headline2").build();

    @Autowired
    private ScraperRunner scraperRunner;
    @Autowired
    private DeMorgenWebsiteScraper deMorgenWebsiteScraper;

    @Autowired
    private ArticleRepository articleRepository;

    @Test
    public void run() {
        scraperRunner.run(deMorgenWebsiteScraper);

        assertThat(articleRepository.findAll()).containsOnly(EXPECTED_ARTICLE_1, EXPECTED_ARTICLE_2);
    }

Maybe you can try @AutoConfigureDataJpa or @AutoConfigureXxx annotations.

Related