如何在主程序中调用 ResponseDefinitionTransformer class

How to call the ResponseDefinitionTransformer in the main class

所以我继续尝试实现 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";
    }

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

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. .withTransformer 除了转换器名称之外还需要参数键和值。消除此编译错误的最简单方法是调用替代方法 .withTransformers("example")

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

    WireMockConfiguration.options()
            .extensions(stub)
            .port(8080)
    
  3. 当您在变压器中定义响应时,存根定义可能很简单:

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