我试图在下面的函数createrequest(temp)中存根一个方法调用的响应。我在单元测试中 Mockito.when(soapMessageFactory.createRequest()).thenReturn(Mockito.mock(SOAPMessage.class))
我需要一个soapmessage.class的示例,但是当前它返回null我有一个mock messagefactory,并将我的测试类注解为, @SpringBootTest
. 我还试着做了一个 when()
对于整个 callSoapService
只是回一个电话 List<String>
但这也没有达到我的预期。单元测试的结果返回大小0并抛出空指针,因为soapmessage为空。
public List<String> callSoapService(String a, String b, List<String> temp) throws SOAPException, IOException, XWSSecurityException{
SOAPMessage request = createRequest(temp);
return null; // dont worry about this for now
}
soapmessagefactory.createmessage()
public SOAPMessage createRequest(String a) throws SOAPException {
SOAPMessage message = soapMessageFactory.createMessage();
message.getMimeHeaders().addHeader("SOAPAction",
testManagerConfiguration.getSOAPUrl() + "/endpoint"); // fails here, null pointer
}
下面是我的单元测试
@Before
public void setUp(){
MockitoAnnotations.openMocks(this);
}
@InjectMocks
private SoapClientImpl soapClient;
@Mock
private MessageFactory soapMessageFactory;
@Test
public void testSOAP() {
Mockito.when(soapMessageFactory.createRequest()).thenReturn(Mockito.mock(SOAPMessage.class));
List<String> result = soapClient.callSoapService("name","b",Arrays.asList("1112567"));
Assertions.assertEquals(result.size(),1);
}
暂无答案!
目前还没有任何答案,快来回答吧!