【问题标题】:@DataJpaTest fails when using Eureka / Feign使用 Eureka / Feign 时 @DataJpaTest 失败
【发布时间】:2017-01-01 22:09:55
【问题描述】:

我有以下 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 注释引起的。

【问题讨论】:

    标签: spring spring-boot spring-data-jpa spring-test


    【解决方案1】:

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

    1. 添加了 bootstrap.yml,内容如下:

      eureka:
        client:
          enabled: false
       spring:
         cloud:
           discovery:
             enabled: false
         config:
             enabled: false
      
    2. 我写了一个测试配置,在测试中引用了:

      @ContextConfiguration(classes = EventServiceApplicationTest.class)
      

      EventServiceApplicationTest 在哪里:

      @SpringBootApplication
      @EnableTransactionManagement(proxyTargetClass = true)
      public class EventServiceApplicationTest {}
      

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

    【讨论】:

      【解决方案2】:

      @EnableDiscoveryClient 也有类似的问题。

      我认为在这种情况下,最简单的方法是放置禁用信息:

      eureka:
        client:
          enabled: false
      

      src/test/resources/application.[properties|yml]。然后在运行测试期间,此配置的优先级高于src/main/resources/application.[properties|yml]

      【讨论】:

        【解决方案3】:

        由于我假设您只是打算测试存储库层,因此您可以从与 Feign 等相关的 ApplicationContext 中过滤掉不需要的 bean。

        您可以通过@DataJpaTestexcludeFilters 属性配置排除 过滤器。

        有关过滤器的详细信息,请参阅Spring Reference Manual

        另一个选项是禁用 Feign 等的自动配置——在这种情况下,您可能会发现这个答案很有用:Disable security for unit tests with spring boot

        【讨论】:

        • 你有例子吗?
        • 我已经更新了我的答案,添加了额外的链接和关于禁用自动配置的评论。
        • 事实上@DataJpaTest 自动禁用自动配置,如here 所述,因此禁用Feign,不需要Eureka。我用完整的堆栈跟踪更新了帖子。无论如何感谢您的帮助。
        • 无论如何我设法禁用了 Eureka 设置属性 eureka.client.enabled=false 但我无法禁用 Feign,使用 @DataJpaTest(showSql = true, excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = FeignAutoConfiguration.class)) 的事件
        【解决方案4】:

        最简单的方法是在你的测试类中添加如下配置:

        @RunWith(SpringRunner.class)
        @TestPropertySource(properties={"eureka.client.enabled=false"})
        @DataJpaTest(showSql = true)
        
        public class BankRepositoryTest {
        
        }
        

        【讨论】:

          猜你喜欢
          • 2019-05-25
          • 2020-11-02
          • 2019-12-20
          • 2017-05-25
          • 2016-05-17
          • 1970-01-01
          • 1970-01-01
          • 2018-10-15
          • 2016-09-20
          相关资源
          最近更新 更多