0

Spring Cloud Dalston을 사용하여 우리는 물론 Zuul을 사용하는 프록시 서비스를 구축했습니다. 나는 이제 계약 준수 관점에서 예상대로 프록시 서비스가 작동하는지 확인하기 위해 Spring Cloud Contract 테스트를 추가하려고한다. 이상한 점은 요청을 보내 200 개의 상태 코드를받을 수 있지만 예상 응답 본문, 내용 유형 헤더 등이 비어있어 결과적으로 테스트가 실패한다는 것입니다.Spring Cloud Contract and Zuul

Zuul 기능을 사용하는 서비스를 테스트하는 데 필요한 Spring Cloud 계약 문서에 추가 구성이 지정되어 있지 않습니까? 여기

+0

대부분의 경우 예를 들어 있습니다. 그런 식으로 시험을 치르시겠습니까? –

+0

DefaultMockMvcBuilder를 사용하여 테스트를 설정합니다. @Before 공개 무효 설치() {예외 DefaultMockMvcBuilder webAppContextSetup MockMvcBuilders.webAppContextSetup = (문맥)를 발생; MockMvc build = webAppContextSetup.build(); RestAssuredMockMvc.mockMvc (빌드); } } –

+0

mock mvc가 아닌 명시 적 모드를 사용해야합니다. 그런 식으로 모든 필터가 등록 될 것입니다. –

답변

0

당신은 Zuul의 경우 일부 컨트롤러 전체 Spring 컨텍스트뿐 아니라 모의 MVC가 필요합니다 https://github.com/spring-cloud/spring-cloud-contract/issues/450

/** 
* Abstraction of configuration to test contracts with external microservices which are using a zuul-gateway in between. 
* 
* When a provider of data only allows communication through a Zuul-Gateway, a special way to ensure valid contracts is needed. 
* The Goal is to emulate the Zuul-Interception to be able to use the contracts stored within the providing service. 
* 
* F.e.: Consumer-Service consumes the Provider-Service, but the provider can not be called directly. 
* The Consumer-Service calls an URL with a prefix consisting of the name of the gateway ("gateway") and name of the 
* service (in this example "Provider-Service"). For example: http://gateway/provider/path/to/resource. 
* The contract provided by Provider-Service holds only a part of the provided URL (here: "/path/to/resource"). 
* The resolution of "gateway" and "provider" is done within this class. 
*/ 
@RunWith(SpringRunner.class) 
@SpringBootTest(classes = EmbeddedZuulProxy.class, 
    properties = {"server.port: 8080", 
     "zuul.routes.provider.path: /provider/**", 
     "zuul.routes.provider.service-id: provider", 
     "zuul.routes.provider.url: http://localhost:9090/" //the url of the provider-stub is at port 9090 
    }, 
    webEnvironment = WebEnvironment.DEFINED_PORT) //defined port is important! Ribbon points to zuul, which then points to the provider-stub 
@AutoConfigureMockMvc 
@AutoConfigureJsonTesters 
//the stub runs on fixed port 9090, so that zuul can point to it 
@AutoConfigureStubRunner(ids = "<group-id>:<artifact-id>:+:stubs:9090") 
@DirtiesContext 
public abstract class ZuulContractBase { 

} 

/** 
* Configuration and Setup of an embedded Zuul-Gateway. 
* This way it is possible to use contracts, stored in providing service 
*/ 
@Configuration 
@ComponentScan(basePackages = "<path.to.feign.client>") //autowiring feign client 
@EnableAutoConfiguration 
@EnableZuulProxy 
@EnableFeignClients 
@RibbonClient(name = "gateway", configuration = SimpleRibbonClientConfiguration.class) 
class EmbeddedZuulProxy { 

    @Bean 
    RouteLocator routeLocator(DiscoveryClient discoveryClient, 
           ZuulProperties zuulProperties) { 
     return new DiscoveryClientRouteLocator("/", discoveryClient, zuulProperties); 
    } 
} 

/** 
* Ribbon Load balancer with fixed server list for "simple" pointing to localhost, 
* which holds the mocked zuul-gateway 
* 
* f.e. a call with feign would normally look like: 
* http://gateway/provider/rest/path/to/your/{url} 
* which is mapped to: 
* http://localhost:{zuulport}/provider/rest/path/to/your/{url} 
*/ 
@Configuration 
class SimpleRibbonClientConfiguration { 

    @Value("${server.port}") 
    private Integer zuulPort; 

    @Bean 
    public ServerList<Server> ribbonServerList() { 
     return new StaticServerList<>(new Server("localhost", zuulPort)); 
    } 
}