将mockito与soapmessage类结合使用

5t7ly7z5  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(284)

我试图在下面的函数createrequest(temp)中存根一个方法调用的响应。我在单元测试中 Mockito.when(soapMessageFactory.createRequest()).thenReturn(Mockito.mock(SOAPMessage.class)) 我需要一个soapmessage.class的示例,但是当前它返回null我有一个mock messagefactory,并将我的测试类注解为, @SpringBootTest . 我还试着做了一个 when() 对于整个 callSoapService 只是回一个电话 List<String> 但这也没有达到我的预期。单元测试的结果返回大小0并抛出空指针,因为soapmessage为空。

  1. public List<String> callSoapService(String a, String b, List<String> temp) throws SOAPException, IOException, XWSSecurityException{
  2. SOAPMessage request = createRequest(temp);
  3. return null; // dont worry about this for now
  4. }

soapmessagefactory.createmessage()

  1. public SOAPMessage createRequest(String a) throws SOAPException {
  2. SOAPMessage message = soapMessageFactory.createMessage();
  3. message.getMimeHeaders().addHeader("SOAPAction",
  4. testManagerConfiguration.getSOAPUrl() + "/endpoint"); // fails here, null pointer
  5. }

下面是我的单元测试

  1. @Before
  2. public void setUp(){
  3. MockitoAnnotations.openMocks(this);
  4. }
  5. @InjectMocks
  6. private SoapClientImpl soapClient;
  7. @Mock
  8. private MessageFactory soapMessageFactory;
  9. @Test
  10. public void testSOAP() {
  11. Mockito.when(soapMessageFactory.createRequest()).thenReturn(Mockito.mock(SOAPMessage.class));
  12. List<String> result = soapClient.callSoapService("name","b",Arrays.asList("1112567"));
  13. Assertions.assertEquals(result.size(),1);
  14. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题