简体   繁体   English

Spring-cloud-contract 测试对 JAXRSCLIENT 不起作用

[英]Spring-cloud-contract testing doesn't work for JAXRSCLIENT

With reference to thread ( Spring Cloud Contract with Jersey ), I was trying to contract test my Jersey controller.参考线程( 与 Jersey 的 Spring Cloud Contract ),我试图对我的 Jersey 控制器进行合同测试。 Unfortunately the maven plugin generates tests that doesn't compile.不幸的是,maven 插件会生成无法编译的测试。

My pom has plugin as below我的pom有如下插件

             <plugin>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-contract-maven-plugin</artifactId>
                <version>2.2.1.RELEASE</version>
                <configuration>
                    <baseClassForTests>com.ing.api.orderapi.contractverifier.ContractVerifierBaseClass</baseClassForTests>
                    <testFramework>JUNIT5</testFramework>
                    <testMode>JAXRSCLIENT</testMode>
                </configuration>
            </plugin>

And the generated test is生成的测试是

@SuppressWarnings("rawtypes")
public class ContractVerifierTest extends ContractVerifierBaseClass {

    @Test
    public void validate_get_products() throws Exception {

        // when:
            Response response = webTarget
                            .path("/products")
                            .request()
                            .build("GET")
                            .invoke();

        // then:
            assertThat(response.getStatus()).isEqualTo(200);
    }

}

The webTarget in the test cannot be resolved.测试中的webTarget无法解析。 Kindly advice.友善的建议。

Please check this sample https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/master/producer_jaxrs请检查此示例https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/master/producer_jaxrs

The code doesn't compile because you haven't defined the webtarget.代码无法编译,因为您尚未定义 webtarget。 Example例子

package com.example;

import java.net.URI;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.UriBuilder;

import com.example.beerapiproducerjaxrs.FraudDetectionController;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.client.ClientConfig;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import static org.springframework.util.SocketUtils.findAvailableTcpPort;

/**
 * Example of using pure Jersey / Jetty API / no Spring to setup the tests.
 */
public class BeerRestBase {
    public static WebTarget webTarget;

    private static Server server;

    private static Client client;

    @BeforeClass
    public static void setupTest() throws Exception {
        int port = findAvailableTcpPort(10000);
        URI baseUri = UriBuilder.fromUri("http://localhost").port(port).build();
        // Create Server
        server = new Server(port);
        // Configure ServletContextHandler
        ServletContextHandler context = new ServletContextHandler(
                ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        // Create Servlet Container
        ServletHolder jerseyServlet = context
                .addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
        jerseyServlet.setInitOrder(0);
        // Tells the Jersey Servlet which REST service/class to load.
        jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
                FraudDetectionController.class.getCanonicalName());
        // Start the server
        server.start();
        ClientConfig clientConfig = new ClientConfig();
        client = ClientBuilder.newClient(clientConfig);
        webTarget = client.target(baseUri);
        try {
            server.start();
        }
        catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    }

    @AfterClass
    public static void cleanupTest() {
        if (client != null) {
            client.close();
        }
        if (server != null) {
            try {
                server.stop();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于 spring-cloud-contract 的自定义存根生成器 - Custom stub generator for spring-cloud-contract 如何通过spring-cloud-contract获得对先前请求的响应 - How to get a response of the previous request with spring-cloud-contract 如何在 spring-cloud-gateway 合约测试中从 spring-cloud-contract 中设置带有 StubRunner 端口的 url - How to set urls with port of StubRunner from spring-cloud-contract in spring-cloud-gateway contract tests mvn clean install对于我的春季云合同消费者测试是成功的,但是当我手动运行测试时,它们失败了吗? - mvn clean install is SUCCESS for my spring-cloud-contract consumer tests, but when I run the tests manually they are FAILING? 使用 spring 云合约测试 Apache Camel servlet - Testing Apache Camel servlet with spring cloud contract Spring Cloud Gateway 不适用于 DiscoveryClientRouteDefinitionLocator - Spring Cloud Gateway doesn't work with DiscoveryClientRouteDefinitionLocator 为什么 spring cloud consul 不能与独立的 tomcat 一起使用? - Why spring cloud consul doesn't work with standalone tomcat? Spring Cloud Gateway 不适用于 @Bean DiscoveryClientRouteDefinitionLocator - Spring Cloud Gateway doesn't work with @Bean DiscoveryClientRouteDefinitionLocator AWS Instance Profile不适用于Spring Cloud AWS - AWS Instance Profile doesn't work with Spring Cloud AWS Spring Cloud Contract 测试适用于 Maven,但不适用于 JUnit - Spring Cloud Contract tests work with Maven but not when run with JUnit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM