wiremock - 使用单个端口创建单个 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 自定义任务?
解决方案
我已经通过使用以下方法解决了:
1)@BeforeClass 和@AfterClass 启动和停止服务器。
2)我尝试使用创建的 JUnit 测试套件和 @BeforeClass 和 @AfterClass 启动和停止服务器,这里将应用套件级别。
推荐阅读
- django - 将日期时间转换为 UTC 以存储在 DateTimeField Django 中
- python - Django 在模板中仅迭代查询集 n 次
- python - 是否可以在 python 中同时运行多个 asyncio?
- javascript - react-router-dom 链接 - 悬停时隐藏 URL
- javascript - jQuery UI 下拉列表更改事件不会触发
- node.js - AWS Lambda - 无法调用第三个资源
- arrays - 线性组合:确定四个系列(光谱)的标量以拟合已知光谱
- node.js - 如何解决 kafkaServices.KafkaServices() 不是 node.js 中的构造函数错误?
- security - AWS Cognito 用户池 ID 和应用程序客户端 ID 是保密的吗?
- swift - 获取核心数据日期作为字符串显示在标签上