How to load specific json files using springboot

206 views
Skip to first unread message

Yuqiang Fan

unread,
Mar 3, 2022, 3:52:14 AM3/3/22
to wiremock-user
Hello,

I am using WireMockRule under springboot test, and my codes look like:

@RunWith(SpringRunner.class)
@SpringBootTest
public class WiremockTest {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity response;
        int portNum = TestUtils.findFreePort(); // it will find a port number
        @Rule
        public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(portNum));

        @Test
        public void test_load1(){
                response = restTemplate.getForEntity(wireMockRule.baseUrl() + "/test/load", String.class);
                assertThat(response.getStatusCode().equals(HttpStatus.OK));
         }
}

These codes work if I put the stub.json for "/test/load"(.json mapping file) under scr/test/resources/mappings, or a subdir (like scr/test/resources/mappings/stubs). This shows that wiremock server will load classpath:mappings/**/*.json by default.

However, what if I want to test the same url under different secnarios? For example, for another unit test, I want to request to the same url, but expect a different response. By achieving this, I might need another stub file like stub2.json to be loaded. Stubfor() is good and can overlap loaded mappings by default, but if the mapping is complicated, a json file is a better and more clean way of stubbing. Thus, I am seeking for assistance on if there is any api or other mean that I can let the wiremock server decide which .json file to be loaded? A api or annotation looks like wireMockRule.loadJsonFile("classpath: secnario/stub2.json") ? Thank you in advnaced.

Best,
Yuqiang
Reply all
Reply to author
Forward
0 new messages