Spring-cloud-contract测试不适用于JAXRSCLIENT

费萨尔汗:

关于线程(与Jersey的Spring Cloud合同),我试图对我的Jersey控制器进行合同测试。不幸的是,Maven插件生成的测试无法编译。

我的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>

生成的测试是

@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);
    }

}

测试中webTarget无法解析。友善的建议。

Marcin Grzejszczak:

请检查此样本https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/master/producer_jaxrs

代码尚未编译,因为您尚未定义webtarget。

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();
            }
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

AWS实例配置文件不适用于Spring Cloud AWS

@TestPropertySource不适用于Spring 1.2.6中带有AnnotationConfigContextLoader的JUnit测试

Spring Boot Cloud Kubernetes配置不适用于多个Pod

Spring安全配置不适用于Spring SimpleUrlHandlerMapping

Spring Cloud Contract测试适用于Maven,但不适用于JUnit

RedirectAttributes不适用于Spring 5

Spring表格不适用于jsp

Spring Boot单元测试不适用于application.yml

Spring Security注销不适用于Spring 4 CORS

适用于Spring Cloud的XACML

没有Spring Framework的Spring Cloud Contract测试(启动)

Hystrix和Turbine不适用于Spring Boot 2和Spring Cloud Finchley.M8

JsonIgnoreProperties不适用于Spring Boot

适用于dotnet / c#的Spring Cloud Configuration客户端

Cloud Firestore不适用于iOS 扑

使用Spring Boot Liquibase测试不适用于多个数据源

@PostFilter不适用于Spring @Aspect

SessionAttributes不适用于Spring Boot

AngularJS模板不适用于Spring

Spring Cloud Contract和普通Spring AMQP

Spring Cloud Contract 不适用于 Surefire 2.20

Wolfram Cloud 不适用于任何浏览器

覆盖的 RabbitSourceConfiguration(应用程序启动器)不适用于 Spring Cloud Edgware

重定向不适用于 Cloud SQL

在非 Spring 项目中运行 Spring Cloud Contract 测试

Spring“cacheNames”不适用于“key”

Firebase 管理 SDK 不适用于 Cloud Scheduler (PubSub)

带有 AttributeConverter 的 Spring 启动测试不适用于 Maven 测试

Spring Cloud 配置不适用全局配置