简体   繁体   中英

Cucumber Test Step time of execution in Java

If i have Cucumber scenario like this :

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

And for this first step in ma TestStep class :

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

If service is not available, test will run for hours. I would like to stop it after some period of time. Any suggestions?

If your lib is not terminating request after provided timeout, you can use alternative approach with ExecutorService . The main idea is submit task with request to SOAP service to ExecutorService and then call get(time, TimeUnit.SECONDS) with timeout from Future.

Example:

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

Solution with ExecutorService can be used but it was not acceptable for my project. I had to override method from my lib. This is what i did:

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

so when i called sendSOAPMessage my test step will break down after 5 seconds if there was no response in that period of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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