1
我試圖啓動測試階段以生成存根。這裏是我的控制器類:春季雲合同:在測試階段無法獲得屬性'serverValue'對空對象
package com.test.controllers;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value = "/temp/temp", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public String testEndpoint(String incoming) {
return "Hello From Test";
}
}
這裏是我的基類:
package com.test.gateway.baseClasses;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
import com.test.controllers.TestController;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RecordStructureBaseClass.Config.class)
public class RecordStructureBaseClass {
@Autowired
WebApplicationContext webApplicationContext;
@Before
public void setup() {
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
}
@Configuration
@EnableAutoConfiguration()
static class Config {
@Bean
TestController controller() {
return new TestController();
}
}
}
這裏是我的合同:
package contracts.record_structure
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method: 'GET'
urlPath 'temp/temp'
}
response {
status 200
body("Hello from Test")
headers {
contentType(applicationJson())
}
}
}
但是,一旦我試圖執行測試階段,我收到以下錯誤:
[DEBUG] Stub content from file [package contracts.record_structure
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method: 'GET'
urlPath 'temp/temp'
}
response {
status 200
body("Hello from Test")
headers {
contentType(applicationJson())
}
}
}
]
[DEBUG] [io.restassured.RestAssured] is not present on classpath. Will not add a static import.
[INFO] Rest Assured version 3.0 found [false]
[DEBUG] Stub content Groovy DSL [Contract(priority:null, request:Request(property:Request$ClientPatternValueDslProperty(), httpMethods:HttpMethods(), method:null, url:null, urlPath:UrlPath(super:Url(queryParameters:null, super:DslProperty(clientValue:temp/temp, server
Value:temp/temp))), headers:null, body:null, multipart:null, matchers:null), response:Response(Response$ServerPatternValueDslProperty(), DslProperty(clientValue:200, serverValue:200), null, Response$ResponseHeaders(), Body(super:DslProperty(clientValue:Hello from Test
, serverValue:Hello from Test)), false, null), label:null, description:null, name:null, input:null, outputMessage:null, ignored:false)]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.582 s
[INFO] Finished at: 2017-09-19T14:30:29+03:00
[INFO] Final Memory: 43M/271M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.cloud:spring-cloud-contract-maven-plugin:1.1.1.RELEASE:generateTests (default-generateTests) on project gateway: Execution default-generateTests of goal org.springframework.cloud:spring-cloud-contract-maven-plugin:1.1
.1.RELEASE:generateTests failed: Cannot get property 'serverValue' on null object -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.cloud:spring-cloud-contract-maven-plugin:1.1.1.RELEASE:generateTests (default-generateTests) on project gateway: Execution default-generateTests of goal org.springframew
ork.cloud:spring-cloud-contract-maven-plugin:1.1.1.RELEASE:generateTests failed: Cannot get property 'serverValue' on null object