我正在使用spring cloud的eureka并假装在一些服务之间进行通信(比方说A和B).现在我喜欢单一测试单个服务的服务层(A).问题是,该服务(A)正在使用假装客户端来请求其他服务的一些信息(B).
在没有任何特殊配置的情况下运行unittes会引发以下异常:java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: service-b=>但我不希望任何服务器运行.
我的问题是:有没有办法嘲笑假装客户端,所以我可以在不运行eureka实例和服务(B)的情况下对我的服务(A)进行单元测试?
编辑:我最终为假装客户端创建了一个存根.存根被标记为主要组件,以强制spring在我的测试中实例化存根.
这是我提出的解决方案.
//the feign client
@FeignClient("user")
public interface UserClient {
UserEntity getUser();
}
//the implementation i use for the tests
@Component
@Primary //mark as primary implementation
public class UserClientTestImpl implements UserClient {
@Override public UserEntity getUser() {
return someKindOfUser;
}
}
Run Code Online (Sandbox Code Playgroud)