使用引导的基于SOAP的Web服务的Junit for Multiple静态端点

时间:2018-06-28 10:28:08

标签: java spring unit-testing spring-boot

我有多个端点和静态wsdl,我的应用程序是使用Spring Boot创建的,我无法找到一种为各种端点编写junit测试用例的方法。

1. SoapServerConfig
@EnableWs
@Configuration
@ComponentScan
public class SoapServerConfig extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext appContext){
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(appContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }

    @Bean(name = "beers")
    public SimpleWsdl11Definition defaultWsdl11Definition(XsdSchema schema){
            SimpleWsdl11Definition simpleWsdl11Definition = new SimpleWsdl11Definition();
        simpleWsdl11Definition.setWsdl(new ClassPathResource("beer.wsdl"));
        return simpleWsdl11Definition;
    }

 @Bean(name = "colddrink")
    public SimpleWsdl11Definition defaultWsdl11Definition(XsdSchema schema){
            SimpleWsdl11Definition simpleWsdl11Definition = new SimpleWsdl11Definition();
        simpleWsdl11Definition.setWsdl(new ClassPathResource("colddrink.wsdl"));
        return simpleWsdl11Definition;
    }



    @Bean
    public XsdSchema beersSchema(){
        return new SimpleXsdSchema(new ClassPathResource("xsd/beers.xsd"));
    }
 @Bean
    public XsdSchema beersSchema(){
        return new SimpleXsdSchema(new ClassPathResource("xsd/colddrink.xsd"));
    }

BeerEndpointIntegrationTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SoapServerConfig.class)
public class BeerEndpointIntegrationTest {

    @Autowired
    private ApplicationContext applicationContext;

    private MockWebServiceClient mockClient;
    private Resource xsdSchema = new ClassPathResource("xsd/beers.xsd");

    @Before
    public void init(){
        mockClient = MockWebServiceClient.createClient(applicationContext);
    }

    @Test
    public void valid_xsd_request_response_test() throws IOException {
        Source requestPayload = new StringSource(
                "<beer:getBeerRequest xmlns:beer=\"http://memorynotfound.com/beer\">" +
                        "<beer:id>1</beer:id>" +
                "</beer:getBeerRequest>");


        Source responsePayload = new StringSource(
                "<ns2:getBeerResponse xmlns:ns2=\"http://memorynotfound.com/beer\"></ns2:getBeerResponse>");

        mockClient
                .sendRequest(withPayload(requestPayload))
                .andExpect(noFault())
                .andExpect(payload(responsePayload))
                .andExpect(validPayload(xsdSchema));
    }

    @Test
    public void id_cannot_be_0_test() throws IOException {
        Source requestPayload = new StringSource(
                "<ns2:getBeerRequest xmlns:ns2=\"http://memorynotfound.com/beer\">" +
                        "<ns2:id>0</ns2:id>" +
                        "</ns2:getBeerRequest>");

        mockClient
                .sendRequest(withPayload(requestPayload))
                .andExpect(serverOrReceiverFault());
    }
}

在示例测试用例中,我设法找到一种区分声明url的方法。

我关注了Spring boot Spring ws security for soap based web service,但这里只有一个网址,我们有多个

0 个答案:

没有答案