我使用spring cloud的尤里卡和假装来进行一些服务之间的通信(可以说A和B)。现在,我想单元测试一个服务的服务层(A)。问题是,这个服务(A)使用假装客户端来请求其他服务的一些信息(B)。模拟一个Eureka Feign客户端进行单元测试
运行单元测试没有任何特殊的配置会抛出以下异常:java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: service-b
=>,但我不希望任何服务器来运行。
我的问题是:是否有办法来嘲笑佯客户端,这样我就可以不运行尤里卡实例和服务(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;
}
}
这是我想出了迄今为止的解决方案: 的佯接口 '@FeignClient( “用户”) 公共接口UserClient { //一些佯注释 UserEntity的getUser(); }' 我使用的测试 '@Component @Primary 公共类UserClientTestImpl实现UserClient { @覆盖 公共UserEntity的getUser(){ 回报someKindOfUser的实施; } }' 基本上它的方法你提到@Markon。 –
你可能会在你的问题中发布代码?在评论中很难阅读代码:D我很高兴它有帮助。当你想测试行为时,你可以使用模拟。如果你想测试“连接”,请设置一个小型网络服务器! :P – Markon