0

我是junit的新手。我需要为以下方法做junit。请指导我

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


        }

     } 

如何在方法中测试 Web 服务?

4

1 回答 1

1

我认为你在测试中非常准确。我认为您不需要模拟 Testbin,因为那是正在测试的类。只需创建一个作为参数传递的日历代理的模拟。

因此,您测试 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);
    }
} 
于 2013-01-24T15:21:06.347 回答