4

With the following setup, spring doesn't pick a transformer defined in JSON file which is under mappings directory, but it does when I declare it directly in test code.

@Configuration
public class WiremockConfiguration {

  @Bean
  WireMockConfigurationCustomizer optionsCustomizer() {
    return new WireMockConfigurationCustomizer() {

      @Override
      public void customize(WireMockConfiguration options) {
        options.extensions(BodyDefinitionTransformer.class);
      }
    };
  }
}
{
  "request": {
    "method": "POST",
    "urlPattern": "/some/thing"
  },
  "response": {
    "status": 200,
    "bodyFileName": "my_payload.json",
    "transformers": [
      "body-transformer"
    ],
    "headers": {
      "Content-Type": "application/json"
    }
  }
}
public class BodyDefinitionTransformer extends ResponseDefinitionTransformer {

  @Override
  public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource files,
      Parameters parameters) {
    return responseDefinition; //checking if this work by putting breakpoint here 
  }

  @Override
  public boolean applyGlobally() {
    return false;
  }

  @Override
  public String getName() {
    return "body-transformer";
  }
}
@ContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource
@AutoConfigureWireMock(port = 9632)
class DummyTestClass extends Specification {

 def "some dummy test" () {
    when:
    stubFor(post("/some/thing").willReturn(aResponse()
        //.withTransformers("body-transformer") transformer work if I declare it in this way
        .withTransformerParameter("test", "test"
        )))

// rest of my test where execute above request
}
}

The code works perfectly when I declare it using .withTransformers("body-transformer") but when I put transformer name into transformers array in JSON file, it doesn't work. Do you have any ideas why?

Kamil W
  • 2,230
  • 2
  • 21
  • 43

0 Answers0