java—从springbootapplicationtest中的应用程序上下文中排除eurekaclient bean

4nkexdtk  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(477)

我正试图用下面的测试配置针对我的spring数据api编写一个集成测试。

eureka:
  client:
    enabled: false
[..] # No other configuration part that affects discovery/eureka client

这是我的测试课

@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@Transactional
class FooAPITest {

  @Test
  void contextLoads() {
  }

}

但是,我有一个组件,它注入eurekaclient以从中获取服务示例

@Component
public class ServiceClient {

  @Autowired
  public ServiceClient(@Qualifier("eurekaClient") EurekaClient eurekaClient) {
    URI serviceUri = URI.create(eurekaClient.getNextServerFromEureka("service", false).getHomePageUrl());
  }

}

因此,在此服务中,我的应用程序无法加载applicationcontext。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.netflix.discovery.EurekaClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value="eurekaClient")}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
    ... 81 more

我已经试过了
我考虑过设置一个自定义contextconfiguration来排除 ServiceClient 因为它在测试类中是不需要的。但是,我需要包括一个配置文件,该文件自动连接entitymanager,但当我使用 @SpringBootApplication(classes = {Configuration.class}) 无法注入entitymanager。此配置如下所示:

@Configuration
class Configuration {
  @Autowired
  EntityManager entityManager;
}

这会产生与entitymanager bean相同的错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
    ... 81 more

当前解决方法
目前我通过嘲笑 ServiceClient 但我想摆脱那种代码的味道。

@MockBean
ServiceClient serviceClient;

@BeforeEach
void setUp() {
  MockitoAnnotations.initMocks(FooAPITest.class);
}

另一个解决方法是将注入的bean标记为不需要,但我发现仅用于测试是不可行的。
解决这个问题的正确方法是什么?

ef1yzkbh

ef1yzkbh1#

你可以试着嘲笑 EurekaClient 在测试中:

@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@Transactional
class FooAPITest {

  @MockBean
  private EurekaClient eurekaClient;

  @Test
  void contextLoads() {
  }

}

这将创建 EurekaClient 就像一个被嘲笑的豆子 ApplicationContext 被注入你的服务。
如果有其他测试初始化Spring ApplicationContext ,您可以在要扫描的应用程序包中创建单独的配置类(使用 @ConditionalOnMissingBean 注解涵盖所有基础):

@Configuration
public class MockEurekaConfiguration {

  @Bean
  @ConditionalOnMissingBean 
  public EurekaClient eurekaClient() {
    return Mockito.mock(EurekaClient.class);
  }

}

相关问题