1

我正在尝试为 A 方法编写测试用例,该方法是handleFault我们soaphandller从中SOAPMessageContext获取接口对象的对象的方法,我必须从中获取SOAPFault对象,并且在获取对象之后,我必须获取它的所有属性,并且我必须创建一个自定义 Exception 类并将SOAPFault对象属性设置为 Exception 类对象并从该方法中抛出该 Exception Class 对象。

我的源代码是CVOSSecurityHandller方法

  public boolean handleFault(SOAPMessageContext context) {
      try {
        Iterator iterator;      
        SOAPFault fault=context.getMessage().getSOAPBody().getFault();
        SoapServiceException serviceException=new SoapServiceException();
    serviceException.setDetail(fault.getDetail());
        serviceException.setFaultActor(fault.getFaultActor());
        iterator=fault.getAllAttributes();
    List<Name> faultAllAttribute=new ArrayList<Name>();
        while(iterator.hasNext()) {
            faultAllAttribute.add((Name)iterator.next());
        }
        serviceException.setFaultAllAttribute(faultAllAttribute);
        serviceException.setFaultCode(fault.getFaultCode());
        serviceException.setFaultCodeName(fault.getFaultCodeAsName());
        serviceException.setFaultCodeQName(fault.getFaultCodeAsQName());
        serviceException.setFaultNode(fault.getFaultNode());
        serviceException.setFaultRole(fault.getFaultRole());
        serviceException.setFaultString(fault.getFaultString());
        iterator=fault.getFaultReasonTexts();
        List<String> faultReasons=new ArrayList<String>();
        while(iterator.hasNext()){
            faultReasons.add((String)iterator.next());
        }
        serviceException.setFaultReasons(faultReasons);
        List<QName> faultSubcode=new ArrayList<QName>();
    while(iterator.hasNext()){
        faultSubcode.add((QName)iterator.next());
        }
    throw serviceException;
    } catch (SOAPException e) {
        e.printStackTrace();
    }
    catch (NullPointerException e) {
    e.printStackTrace();
    }
    return true;
  }

我的自定义异常类

public class SoapServiceException extends RuntimeException {
  private Detail detail;
  private String faultActor;
  private String faultCode;
  private Name faultCodeName;
  private QName faultCodeQName;
  private String faultNode;
  private String faultRole;
  private String faultString;
  private List<QName> faultSubcode;
  private List<Name> faultAllAttribute;
  private List<String> faultReasons;

// here is getter and setter of above properties
}

我的测试用例文件,用于测试 HandleFault 方法

public class CVOSSecurityHandllerTest {
    private CVOSSecurityHandler mockCvosSecurityHandller=mock(CVOSSecurityHandler.class);
    private SOAPMessageContext soapMessageContext=mock(SOAPMessageContext.class);
    private SOAPMessage soapMessage =mock(SOAPMessage.class);
    private SOAPBody soapBody=mock(SOAPBody.class);
    private SOAPFault soapFault=mock(SOAPFault.class);
    @Before
    public void setUp() throws Exception
    {
        Factory.instance = new Factory(new MockServicesModule());
    }

    @After
    public void tearDown() throws Exception
    {
    }
    @Test(expected=SoapServiceException.class)
    public void testHandleFault(){
        try{

        when(soapMessageContext.getMessage()).thenReturn(soapMessage);
            when(soapMessage.getSOAPBody()).thenReturn(soapBody);
            when(soapBody.addFault()).thenReturn(soapFault);
            when(soapFault.getFaultString()).thenReturn("The fault string");
            verify(this.mockCvosSecurityHandller, atLeastOnce()).handleFault(soapMessageContext);
            reset(this.mockCvosSecurityHandller);

        }
        catch(SOAPException e){
            //e.printStackTrace();
        }
        catch(NullPointerException e){
            //throw new SoapServiceException();
        }
        catch(SoapServiceException e){
            e.printStackTrace();
            Assert.assertEquals("The fault",e.getFaultString());
        }


    }
    @BeforeClass
public static void setUpBeforeClass() throws Exception
{
    }

    private class MockServicesModule extends AbstractModule
    {

        @Override
        protected void configure()
        {
            bind(SOAPMessageContext.class).toInstance(CVOSSecurityHandllerTest.this.soapMessageContext);
            bind(SOAPMessage.class).toInstance(CVOSSecurityHandllerTest.this.soapMessage);
            bind(SOAPBody.class).toInstance(CVOSSecurityHandllerTest.this.soapBody);
            bind(SOAPFault.class).toInstance(CVOSSecurityHandllerTest.this.soapFault);
        }
    }


}

所以请帮我看看我在哪里犯了错误并为此提供可能的解决方案。我正在尝试创建模拟对象

CVOSSecurityHandler mockCvosSecurityHandller=mock(CVOSSecurityHandler.class);
private SOAPMessageContext soapMessageContext=mock(SOAPMessageContext.class);
private SOAPMessage soapMessage =mock(SOAPMessage.class);
private SOAPBody soapBody=mock(SOAPBody.class);
private SOAPFault soapFault=mock(SOAPFault.class);

并验证 的模拟对象CVOSSecurityHandller并调用该handleFault方法,但它会抛出 的异常WantedButNotInvoked

4

1 回答 1

0

你的真实对象实例在哪里?我看到的都是嘲笑。您的被测系统(即被测类)应该是一个真实的对象而不是一个模拟对象,否则您的任何真实代码都不会被执行。

于 2012-07-22T19:37:44.410 回答