黄瓜测试在Java中执行的步骤时间

时间:2017-06-01 13:38:52

标签: java web-services testing soap cucumber

如果我有这样的Cucumber场景:

Scenario: Correct response should be returned.
    When User sent "xml example" request
    Then Response should have some data "

对于ma TestStep类的第一步:

@When("^User sent \"(.*)\" request")
    public void sendRequest(String requestName) throws Exception
    {
       //code for SOAP request 
    }

如果服务不可用,测试将持续数小时。我想在一段时间后停止它。有什么建议?

2 个答案:

答案 0 :(得分:0)

如果您的lib在提供超时后未终止请求,则可以使用ExecutorService的替代方法。主要思想是将请求提交给ExecutorService的SOAP服务,然后使用Future的超时时间调用get(time, TimeUnit.SECONDS)

示例:

Future<String> future = executor.submit(new SoapRequeset());
future.get(5L, TimeUnit.SECOND); //will throw exception if result not available after timeout

答案 1 :(得分:0)

可以使用ExecutorService的解决方案,但我的项目无法接受。我不得不从我的lib中覆盖方法。这就是我所做的:

   @Override
    public SOAPMessage sendSOAPMessage(SOAPMessage request) throws Exception
    {
        SOAPConnection soapConnection = SOAPConnectionFactory.newInstance().createConnection();
        URL endpoint =
            new URL(new URL(serviceUrl), "endpoint spec.",
                new URLStreamHandler()
                {
                    @Override
                    protected URLConnection openConnection(URL url) throws IOException
                    {
                        URL target = new URL(url.toString());
                        URLConnection connection = target.openConnection();
                        // Connection settings
                        connection.setConnectTimeout(5000);
                        connection.setReadTimeout(5000);
                        return (connection);
                    }
                });

        return soapConnection.call(request, endpoint);
    }

所以当我打电话给sendSOAPMessage时,如果在那段时间内没有回复,我的测试步骤会在5秒后崩溃。