我正在尝试为soaphandler的handleFault方法创建一个方法的Testcases,因为我们得到了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的异常
所以请尽快回复 感谢
答案 0 :(得分:0)
您的真实对象实例在哪里?我只看到嘲笑。您的被测系统(即被测试的类)应该是真实对象而不是模拟,否则您的实际代码都不会被执行。