【问题标题】:How to write tests on HTTP client apache?如何在 HTTP 客户端 apache 上编写测试?
【发布时间】:2019-02-22 09:11:25
【问题描述】:

我正在尝试为我的服务编写一个测试,该测试与另一个从数据库返回项目的服务建立连接。我的问题是我在测试中设置了连接属性并启动了服务。这怎么可能是模拟的或类似的?

我的启动服务方法:

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

我的设置测试方法和一种测试方法:

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
    }

如何正确做,以免在测试中配置连接?

【问题讨论】:

    标签: java unit-testing junit mocking mockito


    【解决方案1】:

    Javalin 将是模拟真实服务的绝佳工具,因为它允许在测试中进行状态断言。

    Wiremock 也可以使用。但这会导致难以维护行为测试(验证)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      • 2012-01-27
      • 2015-10-17
      相关资源
      最近更新 更多