Spring 合同未达到基础方法 @Before class
Spring Contract does not reach method @Before on base class
我有一个spring合同测试:
public class ContractVerifierTest extends BaseClassForIntegrationTests {
@Test
public void validate_shouldSayHello() throws Exception {
// given:
RequestSpecification request = given()
.header("Content-Type", "application/json");
// when:
Response response = given().spec(request)
.get("/sayhello/Eduardo");
// then:
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.header("Content-Type")).matches("application/json.*");
// and:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).field("['msg']").isEqualTo("hello Eduardo");
}
}
我的基地class看起来像:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@Slf4j
public class BaseClassForIntegrationTests {
@Value("${app.url}") private String url;
@Value("${app.port}") private int port;
@Before
public void setup() {
log.error("Running setup with url:" + url + ":" + port);
RestAssured.baseURI = url;
RestAssured.port = port;
}
}
从未达到 setup
方法,有趣的是,如果我将注释更改为 @BeforeEach
或 @BeforeAll
它会按预期工作。
我有项目的样本here
使用 Contract 3.0.x 默认测试框架是 junit5,您需要显式配置插件以使用 junit 4
我有一个spring合同测试:
public class ContractVerifierTest extends BaseClassForIntegrationTests {
@Test
public void validate_shouldSayHello() throws Exception {
// given:
RequestSpecification request = given()
.header("Content-Type", "application/json");
// when:
Response response = given().spec(request)
.get("/sayhello/Eduardo");
// then:
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.header("Content-Type")).matches("application/json.*");
// and:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).field("['msg']").isEqualTo("hello Eduardo");
}
}
我的基地class看起来像:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@Slf4j
public class BaseClassForIntegrationTests {
@Value("${app.url}") private String url;
@Value("${app.port}") private int port;
@Before
public void setup() {
log.error("Running setup with url:" + url + ":" + port);
RestAssured.baseURI = url;
RestAssured.port = port;
}
}
从未达到 setup
方法,有趣的是,如果我将注释更改为 @BeforeEach
或 @BeforeAll
它会按预期工作。
我有项目的样本here
使用 Contract 3.0.x 默认测试框架是 junit5,您需要显式配置插件以使用 junit 4