1

我目前正在运行一个带有线模拟的测试用例。以下是代码片段:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = DEFINED_PORT)
public class ResolveIT {

 @ClassRule
 public static WireMockRule wireMockRule = new WireMockRule(8900);

 @Autowired
 private TestRestTemplate restTemplate;

 @BeforeClass
 public static void init() {
 wireMockRule.resetAll();
 }

 @Test
 public void testA() {

 HttpEntity<String> entity = new HttpEntity<String>(null, getRequestHeaders());
 ResponseEntity<String> response = restTemplate.exchange(localhost:8080/abc, HttpMethod.GET, entity, String.class);
 assertEquals(response.getStatusCode(), HttpStatus.OK);
 assertEquals(response.getBody(), "Hey");
 }
}

我已将存根放在映射文件夹中的 json 文件中,如下所示:

{
 "request": {
 "method": "GET",
 "url": "/abc/xyz"
 },
 "response": {
 "status": 200,
 "bodyFileName": "body.json",
 "headers": {
 "Content-Type": "application/text",
 }
 }
}

我从我的 IDE 将这个类作为 JUnit 测试运行。现在我需要以下情况的帮助:

我希望运行一个测试套件,其中多个具有此类测试的类将并行运行,简而言之,我希望并行运行wiremock测试。如果我像这样配置我的所有类,我将不得不提供一个不同的端口以使它们并行运行,这在这里似乎不是一个可行的解决方案。另外我在某处读到wiremock不是线程安全的。

  1. 如何使运行线程安全?
  2. 如何配置我的代码,以便wiremock 在一个端口上运行并且我的所有测试都可以并行运行?那应该是什么代码,应该如何配置单个wiremock实例以及应该如何运行测试?
  3. 为了实现这一点,我需要在每个测试类中手动进行存根,还是可以通过 json 文件与动态存根一起实现?PS - 如果需要知道,我在我的项目中使用 maven。
4

0 回答 0