简体   繁体   English

如何在HTTP客户端Apache上编写测试?

[英]How to write tests on HTTP client apache?

I am trying to write a test for my service that sets up a connection with another service that returns me items from the database. 我正在尝试为我的服务编写一个测试,以建立与另一个服务的连接,该服务从数据库返回我的项目。 My problem is that I set the connection properties in the test and start the service. 我的问题是我在测试中设置了连接属性并启动了服务。 How can this be mock or something similar? 这怎么可能是假的或类似的东西?

My start service method: 我的启动服务方法:

public void doStartService() {
        super.doStartService();
        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
        manager.setDefaultMaxPerRoute(maxConnectionsPerRoute);
        manager.setMaxTotal(maxConnections);

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(connectTimeout)
                .setSocketTimeout(socketTimeout)
                .setRedirectsEnabled(false).build();

        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setDefaultRequestConfig(requestConfig);
        builder.setConnectionManager(manager);
        client = builder.build();
    }

My setup test method and one test method: 我的设置测试方法和一种测试方法:

private ProductCatalogIntegrationService service;

 @Before
    public void setup() {
        service = new Service();
        service.setConnectTimeout(10000);
        service.setSocketTimeout(10000);
        service.setMaxConnections(10);
        service.setMaxConnectionsPerRoute(10);
        service.setUrl("http://localhost:8888/products");
        service.doStartService();
    }


    @Test
    public void testReturnProductById() {
        service.setProductById(GET_PRODUCT_BY_ID); // set url from get product by id, by this url my other service goes to the database
        jsonItem = service.getProductById("1"); //get product by id 1

        assertEquals(jsonItem.getId(), FIRST_PRODUCT_ID); // I compare the id on which I made the request to the database, so that I came and was wrapped in a class wrapper
    }

How to do it correctly, so as not to configure the connection in the tests? 如何正确执行此操作,以免在测试中配置连接?

Javalin would be an excellent tool for mocking the real service as it allows for state assertions in your tests. Javalin将是模拟真实服务的绝佳工具,因为它允许在测试中声明状态。

Wiremock can be used as well. 也可以使用Wiremock But it leads to hard to maintain behavioral tests (verify). 但这导致难以维持行为测试(验证)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM