gpt4 book ai didi

java - 如何在与 Spring Boot 的集成测试中 Autowiring 存储库?

转载 作者:行者123 更新时间:2023-11-28 19:56:58 26 4
gpt4 key购买 nike

我正在尝试编写集成测试,但在测试中 Autowiring 存储库时遇到了问题。

我收到这个异常:org.springframework.beans.BeanInstantiationException:无法实例化[org.observer.media.repository.ArticleRepository]:指定的类是一个接口(interface)。

编辑:我用 @EnableJpaRepositories 添加了 PersistenceConfig.class,代码如下。这导致异常 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

我还尝试在 @SpringBootTest(classes = {} 中添加 Application.class 以捕获所有尝试,但这会抛出 Error creating bean with name 'articleRepository': Invocation of init method failed; nested exception is java. lang.IllegalArgumentException:不是托管类型: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 {
}

最佳答案

您只需要提供类,注释为 @Configuration@SpringBootTest

我修改了 here 中的原始示例使用 @SpringBootTest 注解。所以下面的配置有效:

@Configuration
@ComponentScan("hello")
public class AppConfig {

}

注意 @ComponentScan 注释。然后在我的测试中:

@RunWith(SpringRunner.class)
@SpringBootTest(classes={AppConfig.class})
public class CustomerRepositoryTests {
@Autowired
private CustomerRepository customerRepository;
}

它做了一个把戏。您可以尝试在您的示例中执行相同的操作。

关于java - 如何在与 Spring Boot 的集成测试中 Autowiring 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45470877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com