Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring cloud contract: Generated test doesn't have MockMVC configured and fails compile

I am running into this "You haven't configured a MockMVC instance." exception when "mvn clean install".

Running

org.springframework.cloud.contract.verifier.tests.ContractVerifierTest Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.185 sec <<< FAILURE! - in org.springframework.cloud.contract.verifier.tests.ContractVerifierTest validate_shouldGetAmenities(org.springframework.cloud.contract.verifier.tests.ContractVerifierTest) Time elapsed: 0.184 sec <<< ERROR! java.lang.IllegalStateException: You haven't configured a MockMVC instance. You can do this statically

or using the DSL:

given(). mockMvc(..). ..

The thing is, the test that throws this exception is a test that is generated based on a contract.

Here is the contract.

package contracts

org.springframework.cloud.contract.spec.Contract.make {
    request {
        method 'GET'
        url '/abc/def/serviceA?catalog=x'
        body("")
    }
    response {
        status 200
        body(""
        )
        headers {
            contentType(applicationJsonUtf8())
        }
    }
}

Here is the generated test.

package org.springframework.cloud.contract.verifier.tests;

public class ContractVerifierTest {

@Test
public void validate_shouldGetMyStuff() throws Exception {
        // given:
        MockMvcRequestSpecification request = given()
                .body("\"\"");

        // when:
            ResponseOptions response = given().spec(request)
                .get("/abc/def/serviceA?catalog=x");

        // then:
            assertThat(response.statusCode()).isEqualTo(200);
            assertThat(response.header("Content-Type")).matches("application/json;charset=UTF-8.*");
        // and:
            String responseBody = response.getBody().asString();
            assertThat(responseBody).isEqualTo("");
    }

}

Obviously, MockMVC instance is not started in the generated test. The suggestion in the exception message is to start it in the test. However, it is a generated test(under "target" folder). It doesn't matter what I add there, it gets wiped out after "mvn clean install".

Has anyone run into this? Is my contract wrong? That's why it didn't get the MockMVC start part generated in the generated test?

The other concern is the URL in the contract is a simple get. It does return a message in Json format. But for now, I just want to make it very simple by only verifying status code 200 without checking the message.

One more thing about this URL is that I tried this url in postman and get the reponse. I assume this should be the url I use in my contract. Is this assumption wrong? Do I need to form this url in contract differently?

Also, in postman when I request this 'GET' with this url, I don't have anything in "body". That's why I left "body" in contract as empty (""). Is this the right way doing it? Could this affect the generated test?

Any hints will be appreciated.

at   com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.performRequest(MockMvcRequestSenderImpl.java:101)
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.sendRequest(MockMvcRequestSenderImpl.java:296)
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.get(MockMvcRequestSenderImpl.java:367)
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.get(MockMvcRequestSenderImpl.java:47)
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSpecificationImpl.get(MockMvcRequestSpecificationImpl.java:565)
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSpecificationImpl.get(MockMvcRequestSpecificationImpl.java:42)
at
  org.springframework.cloud.contract.verifier.tests.ContractVerifierTest.validate_shouldGetAmenities(ContractVerifierTest.java:23)
like image 845
Eric Avatar asked Feb 23 '17 02:02

Eric


1 Answers

It turns out that I need to add the base class that I am missing(like the FraudBase.java) in the sample. That's where the MockMvc is being instantiated.

like image 171
Eric Avatar answered Nov 11 '22 04:11

Eric