Search code examples
junit4spring-cloud-contract

Spring Contract does not reach method @Before on base class


I have a single spring contract test:

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");
    }

}

My base class looks like:

@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;
    }
}

The setup method is never reached, funny thing, if I change the annotation to @BeforeEach or @BeforeAll it works as expected.

I have a sample of the project here


Solution

  • With Contract 3.0.x the default testing framework is junit5 you need to Configure the plugin explicitly to use junit 4