如果我有这样的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
}
如果服务不可用,测试将持续数小时。我想在一段时间后停止它。有什么建议?
答案 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秒后崩溃。