如何调用主类中的ResponseDefinitionTransformer

时间:2018-05-03 14:10:55

标签: java wiremock

所以我继续尝试实现ResponseDefinitionTransformer,这是我目前的代码:

public class Stub extends ResponseDefinitionTransformer {


    FileOutputStream fos;
    String path;
    File file;
    byte[] message;
    boolean value;


    @Override
    public ResponseDefinition transform(Request rqst, ResponseDefinition rd, FileSource fs, Parameters prmtrs) {


        message = rqst.getBody();
        path = "C:/Users/xxx/Documents/NetBeansProjects/WeatherApplication/xmlFile.xml";
        file = new File(path);


        try {
            FileOutputStream fos = new FileOutputStream(file);
        } catch (FileNotFoundException ex) {
        }


        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException ex) {
            }


            try {
                fos.write(message);
            } catch (IOException ex) {
            }


            try {
                fos.flush();
            } catch (IOException ex) {
            }
            System.out.println("File Written Successfully");
            System.out.println("File Created and Written to");


            value = validateXMLSchema("shiporder.xsd", "xmlFile.xml");


        }

        if(value){
        return new ResponseDefinitionBuilder()
                    .withHeader("Content-Type", "application/xml")
                    .withStatus(200)
                    .withBody("Message Matched")
                    .build();
        }else{
            return new ResponseDefinitionBuilder()
                    .withHeader("Content-Type", "application/xml")
                    .withStatus(404)
                    .withBody("Message not Matched")
                    .build();

        }
    }


    @Override
    public String getName() {
        return "example";
    }

然后在main方法中,这是我的代码,我在“.withTransformer”位中收到错误。我不确定如何将ResponseDefintionTransformer连接到我的实际存根。另外,我不应该调用覆盖方法吗?它如何知道网址请求?我不需要传递网址吗?

public static void main(String[] args) throws IOException, InterruptedException, SAXException, JAXBException, org.xml.sax.SAXException, UnsupportedOperationException {
        Stub sub = new Stub();
        WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options().port(8080));
        wireMockServer.start();



    stubFor(any(urlEqualTo("/user/test")).atPriority(1)
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "application/xml")
                    .withBody("XML RECIEVED")
                    .withTransformer("example")
            )
    );
}

1 个答案:

答案 0 :(得分:0)

  1. .withTransformer除变换器名称外还需要参数键和值。摆脱这种编译错误的最简单方法是调用替代方法.withTransformers("example")

  2. 代码编译后,您仍然无法使变压器工作,因为WireMockServer的实例还不知道您的变压器。您需要调整配置:

    WireMockConfiguration.options()
            .extensions(stub)
            .port(8080)
    
  3. 无论如何,当您在变换器中定义响应时,存根定义可能只是:

    stubFor(any(urlEqualTo("/user/test"))
            .willReturn(aResponse()
                    .withTransformers(stub.getName())
            )
    );
    
  4. 更新:BTW,因为您的变换器是全局的,您甚至不必在存根定义中定义它。它将应用于所有请求。见http://wiremock.org/docs/extending-wiremock/#non-global-transformations