gpt4 book ai didi

spring - 使用 Eureka/Feign 时 @DataJpaTest 失败

转载 作者:行者123 更新时间:2023-12-01 00:37:59 25 4
gpt4 key购买 nike

我有以下 Spring Boot 应用程序(使用 Eureka 和 Feign):

@SpringBootApplication
@EnableFeignClients
@EnableRabbit
@EnableDiscoveryClient
@EnableTransactionManagement(proxyTargetClass = true)
public class EventServiceApplication {

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

和以下测试,用@SpringJpaTest 注释:
@RunWith(SpringRunner.class)
@DataJpaTest(showSql = true)
public class EventRepositoryTest {

@Autowired
private TestEntityManager entityManager;

@Autowired
private EventRepository repository;

@Test
public void testPersist() {
this.entityManager.persist(new PhoneCall());
List<Event> list = this.repository.findAll();

assertEquals(1, list.size());
}
}

在运行测试时,我收到以下错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.netflix.discovery.EurekaClient] found for dependency [com.netflix.discovery.EurekaClient]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

完整的堆栈跟踪 here

有没有办法解决这个问题?我已经看到它是由 @EnableFeignClients 和 @EnableDiscoveryClient 注释引起的。

最佳答案

最后,我设法通过以下方式解决了我的问题:

  • 添加 bootstrap.yml ,内容如下:
    eureka:
    client:
    enabled: false
    spring:
    cloud:
    discovery:
    enabled: false
    config:
    enabled: false
  • 我写了一个测试配置并在测试中引用了它:
    @ContextConfiguration(classes = EventServiceApplicationTest.class)

    其中 EventServiceApplicationTest 是:
    @SpringBootApplication
    @EnableTransactionManagement(proxyTargetClass = true)
    public class EventServiceApplicationTest {}

  • 我不知道是否有最简单的方法,但这有效。

    关于spring - 使用 Eureka/Feign 时 @DataJpaTest 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39145919/

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