Is mocking for unit testing appropriate in this scenario?
Posted
by
Vinoth Kumar
on Programmers
See other posts from Programmers
or by Vinoth Kumar
Published on 2012-10-01T12:19:04Z
Indexed on
2012/10/01
15:52 UTC
Read the original article
Hit count: 371
I have written around 20 methods in Java and all of them call some web services. None of these web services are available yet. To carry on with the server side coding, I hard-coded the results that the web-service is expected to give.
Can we unit test these methods? As far as I know, unit testing is mocking the input values and see how the program responds. Are mocking both input and ouput values meaningful?
Edit :
The answers here suggest I should be writing unit test cases.
Now, how can I write it without modifying the existing code ?
Consider the following sample code (hypothetical code) :
public int getAge()
{
Service s = locate("ageservice"); // line 1
int age = s.execute(empId); // line 2
return age; // line 3
}
Now How do we mock the output ?
Right now , I am commenting out 'line 1' and replacing line 2 with int age= 50
. Is this right ? Can anyone point me to the right way of doing it ?
© Programmers or respective owner