如何使用柑橘在同一端口上运行两个具有不同URL的模拟Web服务?

时间:2016-10-31 12:46:27

标签: java web-services citrus-framework

我正在调查Citrus Framework,以便在项目的测试自动化中使用它。我想运行两个Web服务,让我们来命名:

http://localhost:port/service1
http://localhosr:port/sercice2

然后调用我的SUT(被测系统)。 SUT将同步调用上述两个模拟服务(service1& service2)并返回答案。

我设法做到了,但是在不同的端口上:

  <citrus-ws:server id="helloMockService1"
          port="${server.port1}"
          servlet-mapping-path="/service1"
          auto-start="true"
          timeout="10000"
          endpoint-adapter="genericResponseAdapter1" />

  <citrus-ws:server id="helloMockService2"
          port="${server.port2}"
          servlet-mapping-path="/service2"
          auto-start="true"
          timeout="10000" />

我需要在同一个端口上。我还尝试编写自定义的DispatchingEndpointAdapter,并以某种方式从请求消息中提取上下文路径,但没有成功..

<citrus:dispatching-endpoint-adapter id="dispatchingEndpointAdapter"
         mapping-key-extractor="mappingKeyExtractor"
         mapping-strategy="mappingStrategy"/>

<bean id="mappingStrategy"
  class="com.consol.citrus.endpoint.adapter.mapping.SimpleMappingStrategy">
    <property name="adapterMappings">
      <map>
          <entry key="service1" value-ref="genericResponseAdapter1"/>
          <entry key="service2" value-ref="genericResponseAdapter2"/>
      </map>
    </property>
</bean>

<bean id="mappingKeyExtractor"
  class="com.mycompany.citrus.CustomExtractor">

</bean>

我无法在com.citrus.message.Message类型的请求参数中找到网址。

package com.mycompany.citrus;

import com.consol.citrus.endpoint.adapter.mapping.MappingKeyExtractor;
import com.consol.citrus.message.Message;

public class CustomExtractor implements MappingKeyExtractor{

    @Override
    public String extractMappingKey(Message request) {

        // ther is no URL information in Message object!!!!!!!!!!!!
        return "service1";
    }

}

如何在同一端口上运行Citrus Framework中的两个模拟服务?我希望通过URL来区分它们,而不是有效负载本身......(通过peyload,使用上面的自定义MappingKeyExtractor很容易,因为Message对象包含有效负载)

请帮忙!我不相信Citrus Framework的设计非常糟糕,错过了这样一个基本的测试要求。

1 个答案:

答案 0 :(得分:1)

你快到了。删除 servlet-mapping-path 设置并使用此映射键提取器:

<bean id="mappingKeyExtractor" class="com.consol.citrus.endpoint.adapter.mapping.HeaderMappingKeyExtractor">
    <property name="headerName" value="#{T(com.consol.citrus.http.message.HttpMessageHeaders).HTTP_REQUEST_URI}"/>
</bean>

这将根据请求路径映射传入请求。因此,您可以在简单映射策略中使用 / service1 / service2 键添加映射。