Reputation: 83
I have been trying to mock the feign client call from Services in a spring boot implementation for writing the Junit test cases.
//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);
}
}
Above is not working, I am new to feign client, hence not sure how to mock the feign client interface.
Is there any other way to achieve same thing above.
Upvotes: 0
Views: 7161
Reputation: 587
It probably is working, I assume your Person class does not define hashcode and equals methods? If you haven't defined hashcode and equals methods even assertEquals(new Person("name",12), new Person("name",12)) will fail.
To get your test working you can define hashcode and equals or you could always replace your test method with:
@Test
public someTestClient(){
Person expectedPerson = new Person("name",12));
when(mockPersonClient.getPerson()).return(expectedPerson);
Person person = mockPersionClient.getPerson();
assertEquals(expectedPerson, person);
}
Upvotes: 2