@DataJpaTest 在使用 Eureka / Feign 时失败
@DataJpaTest fails when using Eureka / Feign
我有以下 Spring 引导应用程序(使用 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注解引起的
因为我假设您只是打算测试您的存储库层,您可以从与 Feign 等相关的 ApplicationContext
中过滤掉不需要的 beans
您可以通过 @DataJpaTest
.
的 excludeFilters
属性配置 exclude 过滤器
有关过滤器的详细信息,请参阅 Spring Reference Manual。
另一种选择是禁用 Feign 等的自动配置——在这种情况下,您可能会发现这个答案很有用:
最后我设法通过以下方式解决了我的问题:
添加了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 {}
我不知道是否有最简单的方法,但这行得通。
我与 @EnableDiscoveryClient 有类似的问题。
我认为在这种情况下,最简单的方法是放置禁用信息:
eureka:
client:
enabled: false
在 src/test/resources/application.[properties|yml]
中。然后在测试期间 运行 此配置的优先级高于 src/main/resources/application.[properties|yml]
.
最简单的方法是在测试中添加以下配置class:
@RunWith(SpringRunner.class)
@TestPropertySource(properties={"eureka.client.enabled=false"})
@DataJpaTest(showSql = true)
public class BankRepositoryTest {
}
我有以下 Spring 引导应用程序(使用 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注解引起的
因为我假设您只是打算测试您的存储库层,您可以从与 Feign 等相关的 ApplicationContext
中过滤掉不需要的 beans
您可以通过 @DataJpaTest
.
excludeFilters
属性配置 exclude 过滤器
有关过滤器的详细信息,请参阅 Spring Reference Manual。
另一种选择是禁用 Feign 等的自动配置——在这种情况下,您可能会发现这个答案很有用:
最后我设法通过以下方式解决了我的问题:
添加了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 {}
我不知道是否有最简单的方法,但这行得通。
我与 @EnableDiscoveryClient 有类似的问题。
我认为在这种情况下,最简单的方法是放置禁用信息:
eureka:
client:
enabled: false
在 src/test/resources/application.[properties|yml]
中。然后在测试期间 运行 此配置的优先级高于 src/main/resources/application.[properties|yml]
.
最简单的方法是在测试中添加以下配置class:
@RunWith(SpringRunner.class)
@TestPropertySource(properties={"eureka.client.enabled=false"})
@DataJpaTest(showSql = true)
public class BankRepositoryTest {
}