您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页如何使用Mock/Stub模拟对象来对FeignClient进行

如何使用Mock/Stub模拟对象来对FeignClient进行

来源:二三娱乐

2018.01.15更新

后来在我司架构师的指点下,我改用了一种更优雅友好的方式来对FeignClient对象进行Mock。
首先我们需要一个jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

spring的这个jar包下自带Mock相关内容。只需要在Test类中使用@MockBean声明我们所要Mock的FeignClient对象即可。
如:

@MockBean
private IPromotionController feignPromotionMock

PS:当你需要进一步使用这个对象时,你需要自己写相应的断言。
以上。

--------------------------------------2018.01.15更新分割线------------------------------------------------

前言

该回答提供了一种手动模拟对象的实现方式。至于这种方式属于mock还是stub,就见仁见智了。

扩展阅读:

  • (这篇是提取中心思想翻译过来的《Mocks Aren't Stubs》)

正文

的回答内容如下:

As mentioned before, Mockito is not powerful enough. I solved this with a manual mock.
It's easier than it sounds:

MyService.Java

public class MyService{
    //My service stuff      

    private MyFeignClient myFeignClient;

    @Inject //this will work only with constructor injection
    public MyService(MyFeignClient myFeignClient){
        this.MyFeignClient = myFeignClient
    }


    public void myMethod(){
        myFeignClient.remoteMethod(); // We want to mock this method
    }
}

MyFeignClient.Java

@FeignClient("target-service")
public interface MyFeignClient{

    @RequestMapping(value = "/test" method = RequestMethod.GET)
    public void remotemethod();
}

If you want to test the code above while mocking the feignclient, do this:

MyFeignClientMock.java

@Component
public class MyFeignClientMock implements MyFeignClient {

    public void remoteMethod(){
         System.out.println("Mocked remoteMethod() succesfuly");
    }
}

MyServiceTest.java

@RunWith(SpringJUnit4ClassRunner.class)
public class MyServiceTest {

    private MyService myService;

    @Inject
    private MyFeignClientMock myFeignClientMock;

    @Before
    public void setUp(){
       this.myService = new MyService(myFeignClientMock); //inject the mock
    }

    //Do tests normally here...
}

补充和说明

上面的答案可以很好地实现对FeignClient的mock,但我们需要作进一步的补充,如下。具体的修改原因随后附上。

MyService.Java

@Service
public class MyService{
    //My service stuff      
    @Autowired
    private MyFeignClient myFeignClient;
    @Autowired
    private MyRepository myRepository;

    @Autowired
    public MyService(MyFeignClient myFeignClient,MyRepository myRepository){
        this.myFeignClient = myFeignClient;
        this.myRepository = myRepository;
    }


    public void myMethod(){
        myFeignClient.remoteMethod(); // We want to mock this method
        myRepository.findAll();
    }
}

MyFeignClient.Java

@FeignClient("target-service")
public interface MyFeignClient{

    @RequestMapping(value = "/test" method = RequestMethod.GET)
    public void remotemethod();
}

MyFeignClientMock.java

@Component
public class MyFeignClientMock implements MyFeignClient {

    public void remoteMethod(){
         System.out.println("Mocked remoteMethod() succesfuly");
    }
}

MyServiceTest.java

@RunWith(SpringJUnit4ClassRunner.class)
public class MyServiceTest {

    private MyService myService;

    @Autowired
    private MyFeignClientMock myFeignClientMock;
    @Autowired
    private MyRepository myRepository;

    @Before
    public void setUp(){
       this.myService = new MyService(myFeignClientMock,myRepository); //inject the mock
    }

    @Test
    public void Test(){
        myService.myMethod();
    }

    //Do  other tests normally here...
}

说明:

  • MyService.java中原文可能漏掉了@Service注解,在此做了补充。

  • 【重要】:通过构造函数new出来的service对象,没有在构造函数中初始化的其他注入会为空。
    在此我特地在MyService.java中注入了MyRepository并修改了相应构造函数进行示例。
    如果构造函数中像原文一样只传入MyFeignClient的实现,那么由于MyRepository没有被初始化,在调用myMethod()时会出现NullPointerException。
    同时,这也提现了这种实现方式的一个弊端:对注入对象多的Service不友好。望周知。


以上。
希望我的文章对你能有所帮助。
我不能保证文中所有说法的百分百正确,但我能保证它们都是我的理解和感悟以及拒绝复制黏贴。
有什么意见、见解或疑惑,欢迎留言讨论。

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务