Spring云契约:在测试阶段无法在null对象上获取属性'serverValue'

时间:2017-09-19 11:34:09

标签: spring-cloud-contract

我正在尝试启动测试阶段以生成存根。这是我的控制器类:

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

1 个答案:

答案 0 :(得分:0)

我认为你应该写method(GET())method("GET")而不是设置地图`方法:'GET'。

我不知道是谁贬低了这个答案,但作为这个框架的创造者,我或多或少知道什么是编译而不是;)没有地图“方法”,有一个名为method的方法。