2013-01-24 144 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); 
    } 
}