首页 > 解决方案 > 使用单个端口创建单个 WireMockServer 对象

问题描述

我对 Wire mock 和 gradle 很陌生。我计划通过使用单个 WireMockServer 对象和使用 8081 端口及其在 gradle 任务中可配置来进行设置。

构建.gradle

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'com.williamhill.wiremock' version '0.4.1'
    id 'java'
}

wiremock {
    dir "src/test/resources/"
    params "--port=8081"
}

task integrationTest(type: Test) {
    runWithWiremock = true
}

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = GladletaskApplication.class)
@AutoConfigureWireMock
@ActiveProfiles(value = "integration")
public class StudentControllerTest {

    @Rule
    public WireMockRule wireMockRule = new WireMockRule();

    @Autowired
    public TestRestTemplate testRestTemplate;

    @Before
    public void setUp() {
        mockRemoteService();
    }
    @Test
    public void testAllStudents() {
        ResponseEntity<List<Student>> responseEntity = testRestTemplate.exchange("http://localhost:8093/all", HttpMethod.GET,null,
                new ParameterizedTypeReference<List<Student>>(){}
        );
        List<Student> studentList = responseEntity.getBody();
        System.out.println("StudentList--->" + studentList.size());
    }

    private void mockRemoteService() {
        stubFor(get(urlEqualTo("/all-students"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "application/json")
                        .withBodyFile("response.json")
                ));
    }
}

现在的目标是我只启动一次 8081 服务器并运行所有测试用例并最后关闭

有可能吗?能否请提供 springboot-wiremock gradle 自定义任务?

标签: wiremockspring-boot-gradle-plugingradle-taskwiremock-standalonewiremock-record

解决方案


我已经通过使用以下方法解决了:
1)@BeforeClass 和@AfterClass 启动和停止服务器。
2)我尝试使用创建的 JUnit 测试套件和 @BeforeClass 和 @AfterClass 启动和停止服务器,这里将应用套件级别。


推荐阅读