Spring Cloud Service Unit Testing Strategy

Given the following Spring cloud setup: A data-servicewith database access, eureka-servicefor registry processing and service discovery, and a third service business-service, which will be one of the various services that encapsulate business cases.

Testing the device is data-servicenot a problem, I just turn off eureka through

eureka.client.enabled=false

and use the in-memory database for my tests.

To access data-servicefrom business-service, I use @FeignClient("data-service")an annotated interface with the name DataClient, which if necessary @Autowired. The service is open to Eureka if both work. This is great for a production installation with all services running.

But now I want to unit test some functions of mine business-service. It would be nice to run a test service with

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
@SpringApplicationConfiguration(classes = Application.class)

how am i doing in data-service. The problem is opening Eureka-dependent mine FeignClient... So my test class crashes because auto-installation of my DataClient-instance does not work.

How can I tell Spring to use a fake instance DataClientonly for my tests? Or is this the only way to get my tests to run an available, running instance data-serviceand my Eureka server?

+3
source share
3 answers

1, config bean, feignclient , true "eureka.enabled"

@Configuration
@EnableDiscoveryClient
@EnableFeignClients
@ConditionalOnProperty(name = "eureka.enabled")
public class EurekaConfig {
}

2, eureka , application-test.yml

eureka:
     enabled: false

3, maven, feign, :

@Service
public class DataServiceImpl implements DataService {}

, unit test

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@IntegrationTest({"server.port=0", "management.port=0",    "spring.profiles.active=test"})
public abstract class AbstractIntegrationTests {}

spring.

unit test, mockito set , mock-

+12

- ... , @Configuration Conf, DataClient :

@Bean
@Primary
public DataClient getDataClient() {
    ...
}

@SpringApplicationConfiguration(classes = {Application.class, Conf.class})

.

+1

Yunlong .

, , "basePackages" @EnableFeignClients @FeignClient.

com.feign.client.FeignClient.class

@FeignClient(value = "${xxxx}")
public interface FeignClient {
}

com.feign.config.EurekaConfig.class

@Configuration
@EnableFeignClients(basePackages = {"com.feign.client"})
@EnableEurekaClient
@ConditionalOnProperty(name = "eureka.enabled")
public class EurekaClientConfig {
}

Ps. , .

0

Source: https://habr.com/ru/post/1678992/


All Articles