Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy for unit testing a Spring Cloud Service

Given the following Spring Cloud setup: A data-service with access to a database, an eureka-service to handle service registry and discovery and a third service business-service which will be one of various services which encapsulate business cases.

Unit testing the data-service is no problem, I just turn off eureka via

eureka.client.enabled=false

and use an in-memory database for my tests.

To access the data-service from business-service, I'm using an @FeignClient("data-service") annotated interface named DataClient which is @Autowired where needed. The service is discovered by Eureka, if both are running. This works fine for a production-like setup with all services running.

But now I want to unit test some features of my business-service. It wouldn't be a problem to start a test service with

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

like I'm doing in data-service. The problem is the Eureka-dependent discovery of my FeignClient... So my testclass crashes, because the autowiring my DataClient-instance doesn't work.

Am I abled to tell Spring to use a faked instance of DataClient just for my tests? Or is the only way to get my tests running an accessible, running instance of data-service and my Eureka server?

like image 628
justus Avatar asked Nov 30 '22 16:11

justus


1 Answers

1, first create config bean, let the discovery client and feignclient only work when "eureka.enabled" is true

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

2, disable the eureka config for test profile, so in application-test.yml

eureka:
     enabled: false

3, my project is build by maven, so i create a implement for my feign client interface, for example:

@Service
public class DataServiceImpl implements DataService {}

after this, when you run test in unit test with

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

the fake service will inject in to spring context.

Or for normal unit test case, you can just need mockito mock the service class and use set method or construct method inject the mock object in your class

like image 169
Yunlong Avatar answered Dec 17 '22 00:12

Yunlong