Search code examples
spring-cloud-contract

Base class no longer working after upgrading to Spring Cloud Contract 1.2.0


I am in the process of upgrading to Spring Cloud Edgware.RELEASE, and I've got a question about how to properly set up a base class for Spring Cloud Contract tests. Following is what I have currently as a base class that works through Dalston.SR5:

import javax.servlet.Filter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;

@RunWith(SpringRunner.class)
@ActiveProfiles("local")
@SpringBootTest(classes = {Bootstrap.class})
@DirtiesContext
public class ConsumerDrivenContractTests {

  @Autowired
  private WebApplicationContext applicationContext;

  @Autowired
  private Filter springSecurityFilterChain;

  @Test
  public void generateTestsFromGroovyFiles() {

  }

  @Before
  public void setup() {
    DefaultMockMvcBuilder defaultMockMvcBuilder =
        MockMvcBuilders.webAppContextSetup(applicationContext).addFilter(springSecurityFilterChain);
    MockMvc mockMvc = defaultMockMvcBuilder.build();
    RestAssuredMockMvc.mockMvc(mockMvc);
  }

}

Upon upgrading to Edgware.RELEASE, my import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc; no longer resolves, which is confusing. The Spring Cloud Contract documentation states "by default, Rest Assured 3.x is added to the classpath" (see http://cloud.spring.io/spring-cloud-static/spring-cloud-contract/1.2.0.RELEASE/single/spring-cloud-contract.html#maven-rest-assured), and the example given shows how to use Rest Assured 2.x with the <groupId>com.jayway.restassured</groupId> dependency. However, the <artifactId>spring-cloud-starter-contract-verifier</artifactId> for 1.2.0.RELEASE pulls in the <groupId>io.rest-assured</groupId> dependencies. Given the documentation, I was expecting the com.jayway... jars to be resolved.

Is my base class approach still valid upon upgrading to 1.2.0.RELEASE, and if so, do I need to explicitly add the com.jayway... dependencies to my pom.xml file? If so, it would be helpful if the documentation stated this.


Solution

  • By default, Rest Assured 3.x is added to the classpath. RestAssured 3.x. has imports io.restassured and they show up in your generated tests. You, in your base class have com.jayway which is RestAssured 2.x. So your generated tests require you to use io.restassured imports in your base class. So either you fix your base class to use Rest Assured 3.x or you have to provide an explicit dependency to Rest Assured 2.x. in your plugin to fix the imports in the generated tests.