I have more than 2 contract test in same groovy test file. When I am executing the contract test using spring cloud contract test framework on the provider side, it always take the last contract test from the groovy test file and ignore the first one when generating ContractVerifierTest.java file.
Please find below contract test code:
package contracts
import org.springframework.cloud.contract.spec.Contract
Contract.make {
name("contract test 1")
description "should return status 200 OK"
request {
url "/endpoint1"
method GET()
}
response {
status OK()
headers {
contentType applicationJson()
}
body (
sampleValue: '12345'
)
}
}
Contract.make {
name("contract test 2")
description "should return status 201 OK"
request {
url "/endpoint2"
method PUT()
}
response {
status CREATED()
headers {
contentType applicationJson()
}
}
}
Generated contractVerifierTest.java
Here we can see that we have the last contract test present in this autogenerated file and contract test 1 is ignored.
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import io.restassured.module.mockmvc.specification.MockMvcRequestSpecification;
import io.restassured.response.ResponseOptions;
import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat;
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.*;
import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;
@SuppressWarnings("rawtypes")
public class ContractVerifierTest extends ContractVerifierBase {
@Test
public void validate_contracttest() throws Exception {
// given:
MockMvcRequestSpecification request = given();
// when:
ResponseOptions response = given().spec(request)
.get("endpoint2");
// then:
assertThat(response.statusCode()).isEqualTo(201);
assertThat(response.header("Content-Type")).matches("application/json.*");
}
Build.gradle:
contracts {
contractsDslDir = file("src/contractTest/resources/contracts/")
packageWithBaseClasses = 'com.contracts'
baseClassForTests = 'com.contracts.ContractVerifierBase'
baseClassMappings {
baseClassMapping(".*com.contracts.*", "com.contracts.ContractVerifierBase")
}
}
contractTest {
useJUnitPlatform()
testLogging{
events "Passed", "Skipped", "Failed"
}
}
ContractVerifierBase.java
import com.ServiceApplication;
import com.exceptions.ResourceNotFoundException;
import com.exceptions.SystemIntegrationException;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
@SpringBootTest(classes = ServiceApplication.class)
@ActiveProfiles("local")
public abstract class ContractVerifierBase {
@BeforeEach
public void setup() throws SystemIntegrationException, ResourceNotFoundException {
RestAssuredMockMvc.standaloneSetup();
}
}
Tried to have multiple contracts in the same groovy test file but it did not work.