Reputation: 1
I am newbie to junit. I need to do junit for the following method. kindly guide me
public boolean binlist(params hpproxy, calendarparam cpxproxy)
{
Getbinresponse binresponse;
cpproxy.setid(hpproxy.getId());
binresponse= cpproxy.getBinlist(); // resturns a list calling webservice
if (binresponse.size>0)
{
result=true;
}
else
{
result=false;
}
return result;
}
I have tried to test the binlist method using mock object.
class testbin
{
@test
public void testbinlist()
{
Testbin mocktestbin=mock(testbin.class);
calendarproxy cpproxy=mock(calendarproxy.class);
params hpproxy= mock(cparams.class);
hpproxy.setId("123");
stub(cpproxy.getBinList()).toReturn(gettestbins()) // mocked getbinlist()
boolen result= mocktestbin.binlist();
assertTrue(result);
}
}
how to test the webservice inside a method?
Upvotes: 0
Views: 3210
Reputation: 4246
I think you are pretty spot on in your test. I think you do not need to mock the Testbin since that is the class under test. Just create a mock of the calendarproxy that is being passed on as an argument.
So your test method to test bin would look something like what is below.
class testbin
{
@test
public void testbinlist()
{
Testbin mocktestbin= new Testbin();
calendarproxy cpproxy=mock(calendarproxy.class);
params hpproxy= mock(cparams.class);
hpproxy.setId("123");
when(cpproxy.getBinList()).thenReturn(gettestbins()); // mocked getbinlist()
boolen result= mocktestbin.binlist(hpproxy,cpproxy);
assertTrue(result);
}
}
Upvotes: 1