2013-01-24 143 views
0

我是junit的新手。 我需要为以下方法做junit。请亲引导我junit测试调用webservice的方法

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;  
     } 

我试过使用模拟对象来测试binlist方法。

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); 


     } 

    } 

如何测试webservice里面的方法?

+0

请仅仅执行'return binresponse.size> 0;'而不是所有那些如果其他等它只是读了很多更好:) – blank

回答

1

我认为你在你的测试中很有潜力。我认为你不需要模拟Testbin,因为那是被测试的类。只需创建一个作为参数传递的calendarproxy的模拟。

所以你测试bin的测试方法看起来像下面的内容。

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); 
    } 
}