[英]How autowire a repository in an integration test with Spring Boot?
我正在嘗試編寫集成測試,但在測試中自動裝配存儲庫時遇到問題。
我收到這個異常:org.springframework.beans.BeanInstantiationException:無法實例化[org.observer.media.repository.ArticleRepository]:指定的類是一個接口。
編輯:我添加了 PersistenceConfig.class 和 @EnableJpaRepositories,代碼如下。 這導致異常org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
我還嘗試在 @SpringBootTest(classes = {} 中添加 Application.class in a catch all 嘗試,但拋出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
created 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(配置類只包含域類的bean):
@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);
}
存儲庫:
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 {
}
您只需要向@SpringBootTest
提供注釋為@Configuration
的類。
我已經修改了 此處的原始示例以使用@SpringBootTest
注釋。 因此,以下配置有效:
@Configuration
@ComponentScan("hello")
public class AppConfig {
}
注意@ComponentScan
注釋。 然后在我的測試中:
@RunWith(SpringRunner.class)
@SpringBootTest(classes={AppConfig.class})
public class CustomerRepositoryTests {
@Autowired
private CustomerRepository customerRepository;
}
它做了一個伎倆。 你可以嘗試在你的例子中做同樣的事情。
我有同樣的異常org.springframework.beans.BeanInstantiationException: Failed to instantiate [...]: Specified class is an interface on my integration test。 我在不使用 PersistenceConfig.class 的情況下解決了問題,但只更改了我的集成類中使用的錯誤注釋。 我使用了以下注釋:@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) 因為我不想使用內存數據庫。
您的代碼可能如下所示:
@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);
}
也許您可以嘗試@AutoConfigureDataJpa
或@AutoConfigureXxx
注釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.