источник ответа полезной нагрузки с пружиной MockWebServiceClient - PullRequest
4 голосов
/ 07 июня 2011

Я использую spring-ws 2.0.2 и spring-ws-test для запуска интеграционных тестов моего SOAP-сервера. Я использую такой подход, как http://static.springsource.org/spring-ws/site/apidocs/org/springframework/ws/test/server/MockWebServiceClient.html

Вот код, который я запускаю с ожидаемым ответом XML, опущенным для краткости и потому что он не имеет отношения к вопросу.

Я бы хотел видеть xml в полезной нагрузке ответа, но не могу понять, как получить к нему доступ. Если я установлю точку останова после того, как ответ будет установлен, и проверим его, я увижу, что у него есть закрытый messageContext.response типа SaajSoapMessage, но я не могу понять, как получить к нему доступ или есть лучший способ увидеть XML-ответ.

package com.example.integration;                                                                     

import static org.springframework.ws.test.server.RequestCreators.*;                                 
import static org.springframework.ws.test.server.ResponseMatchers.*;                                
import static org.testng.AssertJUnit.*;                                                             

import javax.xml.transform.Source;                                                                  

import org.springframework.beans.factory.annotation.Autowired;                                      
import org.springframework.context.ApplicationContext;                                              
import org.springframework.ws.test.server.MockWebServiceClient;                                     
import org.springframework.ws.test.server.ResponseActions;                                          
import org.springframework.xml.transform.StringSource;                                              
import org.testng.annotations.BeforeClass;                                                          
import org.testng.annotations.Test;                                                                 

@ContextConfiguration(locations={"/transaction-test-context.xml", "/spring-web-services-servlet.xml"})
public class BaseWebServiceIntegrationTest {                          

    @Autowired
    private ApplicationContext applicationContext;

    private MockWebServiceClient mockClient;

    @BeforeClass(groups="integration")
    public void createClient() {
        assertNotNull("expected applicationContext to be non-null", applicationContext);
        mockClient = MockWebServiceClient.createClient(applicationContext);
    }

    @Test(groups="integration")
    public void proofOfConcept() {

        assertNotNull("expected mockClient to be non-null", mockClient);

        Source requestPayload = new StringSource(
                "<s0:ListDevicesRequest " +
                "xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' " +
                "xmlns:xs='http://www.w3.org/2001/XMLSchema' " +
                "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
                "xmlns:s0='urn:com:example:xmlschema:service:v1.1:DeviceManager.xsd'>" +
                    "<s0:MacID>66:bb:be:ee:00:00:01:00</s0:MacID>" +
                "</s0:ListDevicesRequest>"
                );

        Source responsePayload = new StringSource("<!-- response omitted for the post, not relevant -->");

        ResponseActions response = mockClient.sendRequest(withPayload(requestPayload));
        response.andExpect(noFault())
                .andExpect(payload(responsePayload));

    }
}

отредактировано для добавления файла контекста приложения в соответствии с запросом. у нас есть несколько, но ниже приводится специфическая для мыла.

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sec="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <sec:global-method-security pre-post-annotations="disabled" jsr250-annotations="disabled" />

    <tx:annotation-driven/>
    <context:component-scan base-package="com.example.integration"/>
    <context:component-scan base-package="com.example.signal"/>

    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

    <bean id="faultService" class="com.example.webservice.fault.FaultService"/>

    <bean id="soapDeviceService" class="com.example.webservice.device.SoapDeviceServiceImpl"/>


    <!-- 1.1 Endpoints -->
    <bean id="deviceManagerEndpoint_v1_1" class="com.example.webservice.spring.DeviceManagerEndpoint">
        <property name="soapDeviceService" ref="soapDeviceService"/>
    </bean>



    <bean class="com.example.webservice.spring.PayloadMethodMarshallingEndpointAdapter">
        <property name="marshaller" ref="marshaller"/>
        <property name="unmarshaller" ref="marshaller"/>
    </bean>
    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="mtomEnabled" value="false"/>
        <property name="contextPath" value="com.example.xmlschema.service.v1_0.devicemanager"/>
    </bean>

    <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
        <property name="interceptors">
            <list>
                <bean class="com.example.webservice.interceptor.LoggingInterceptor"/>
                <ref bean="validatingInterceptor"/>
            </list>
        </property>
    </bean>

    <bean id="validatingInterceptor" class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
        <property name="schemas">
            <list>
                <value>/WEB-INF/wsdl/*Types_v*.xsd</value>
            </list>
        </property>
        <property name="validateRequest" value="true"/>
        <property name="validateResponse" value="true"/>
    </bean>

    <bean class="com.example.webservice.spring.SpringWebserviceFaultMappingExceptionResolver">
        <property name="order" value="1"/>
        <property name="marshaller" ref="marshaller"/>
    </bean>

</beans>

Ответы [ 4 ]

6 голосов
/ 26 июня 2015

Хорошо, я опоздал на 4 года, но вот мой ответ, которым я и горжусь, и стыдюсь: -

.andExpect(
    new ResponseMatcher()
    {
        @Override
        public void match(WebServiceMessage request, WebServiceMessage response)
            throws IOException, AssertionError
        {
            response.writeTo(System.out);
        }
    })

А с Java 8:

.andExpect((request, response) -> response.writeTo(System.out))
2 голосов
/ 19 декабря 2011

Вы можете установить PayloadLoggingInterceptor в вашем контексте spring-ws, который по умолчанию будет регистрировать все полезные нагрузки запросов и ответов.Настройте его следующим образом:

<sws:interceptors>
  <bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
</sws:interceptors>

Для получения дополнительной информации обратитесь к документации springsource здесь

1 голос
/ 14 июля 2011

Сконфигурируйте log4j и добавьте конфигурацию в log4j, чтобы показать журналы классов пружин.В журналах будет показан порядок вызова классов Spring, а также отправлены запрос и ответ.

<logger name="org.springframework.ws" additivity="false">
   <level value="DEBUG#org.tiaa.infra.logging.TiaaLevel" />
   <appender-ref ref="DailyRollingFileAppender"/>
</logger>
0 голосов
/ 28 апреля 2016

+ 1 для решения @NeilStevens ... Никогда не поздно! Спасибо :-) Вот небольшой рефакторинг для распечатки материала в логгер sf4j и использования магии статического импорта ...:

import static SomeTestClass.ResponseOutputMatcher.printResponse;


public class SomeTestClass {
    private static final Logger LOG = LoggerFactory.getLogger(SomeTestClass.class);

    @Test
    public void someTest() {

        mockClient.sendRequest(...)
          .andExpect(printResponse(LOG))
          .andExpect(noFault());
    }

    @RequiredArgsConstructor
    public static class ResponseOutputMatcher implements ResponseMatcher {
        private final Logger logger;

        public ResponseOutputMatcher() {
            this(LoggerFactory.getLogger(ResponseOutputMatcher.class));
        }

        @Override
        public void match(WebServiceMessage request, WebServiceMessage response) throws IOException, AssertionError {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.writeTo(out);
            logger.info(format("Received payload response:\n%s\n%s\n%s", repeat(">", 80), out.toString(), repeat("_", 80)));
        }

        public static ResponseMatcher printResponse(Logger logger) {
            return new ResponseOutputMatcher(logger);
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...