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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With