【问题标题】:Feign client mocking in in Junit implementation在 Junit 实现中模拟客户端
【发布时间】:2019-05-10 11:38:19
【问题描述】:

我一直在尝试在 Spring Boot 实现中模拟来自服务的 feign 客户端调用,以编写 Junit 测试用例。

//Service code
@Autowired
private PersonClient personClient;

//Using the personClient
public Person someMethod(){
    //Interface defined with URL and all defination
    Person person = personClient.getPerson();
}

//Service testing bean

@RunWith(MockitoJUnitRunner.class)
public Class ServiceTest{

    @Mock
    public PersonClient mockPersonClient;

    @Test
    public someTestClient(){

        when(mockPersonClient.getPerson()).return(new Person("name",12));
        Person person = mockPersionClient.getPerson();
        assertEquals(new Person("name",12), person);

    }

}

上述方法不起作用,我是 feign 客户端的新手,因此不知道如何模拟 feign 客户端界面。

有没有其他方法可以实现上述相同的事情。

【问题讨论】:

    标签: spring-boot junit mockito spring-cloud-feign feign


    【解决方案1】:

    它可能正在工作,我假设您的 Person 类没有定义 hashcode 和 equals 方法?如果你还没有定义 hashcode 和 equals 方法,甚至 assertEquals(new Person("name",12), new Person("name",12)) 都会失败。

    为了让您的测试正常工作,您可以定义 hashcode 和 equals,或者您可以随时将您的测试方法替换为:

    @Test
    public someTestClient(){
        Person expectedPerson = new Person("name",12));
        when(mockPersonClient.getPerson()).return(expectedPerson);
        Person person = mockPersionClient.getPerson();
        assertEquals(expectedPerson, person);
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      相关资源
      最近更新 更多