我对Wire mock和gradle是个新手,我打算使用8081端口的WireMockServer对象进行设置,并在gradle任务中进行配置。我打算使用8081端口的WireMockServer对象进行设置,并在gradle任务中进行配置。
build.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启动和停止服务器,这里去应用套件级别。