Java 功能测试 Http 回调
Java Functional Testing Http Callback
我正在使用 Cucumber 来测试部署到容器中的服务。该请求包含一个 URL 操作成功时调用的服务。创建等待 Http 回调的 Cucumber 测试的好方法是什么? Gherkin 脚本看起来像这样。
Scenario: Process Order
Given An Order has been submitted
When the Order is processed
Then the order process service calls back with a successful status message
Java 胶水代码是什么样的?
这是我使用嵌入式 http 服务器想出的解决方案。在 OrderSteps.java 胶水代码中,我添加了一个 class 用于在另一个线程上启动服务器。
private static class Callback implements Runnable
{
public void run()
{
HttpServer server;
try
{
server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/callback", new CallbackHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
catch (IOException e)
{
logger.debug("HTTP server loop failure.", e);
}
}
static class CallbackHandler implements HttpHandler
{
@Override
public void handle(HttpExchange t) throws IOException
{
// Read the message and set the global variable
// which informs the main test thread a callback
// has been received.
InputStream is=t.getRequestBody();
byte[] buf=new byte[1000];
int len=is.read(buf);
OrderSteps.receivedCallback=new String(buf,0,len);
String response = "Callback received.";
t.sendResponseHeaders(200, response.length());
// Send response
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
然后在OrderStepsclass中,在发布Order的step/method中,启动服务器线程,然后提交订单。这保证了服务器将收到回调,因为它在订单提交之前正在监听。
// Start a listener for the callback.
Thread callbackThread = new Thread(new Callback());
callbackThread.start();
在检查是否收到回调的 step/method 中,有一个循环检查静态变量以查看它是否已被设置。
// Allow 5 seconds for the callback to occur.
for (int i = 0; i < 5; i++)
{
if (receivedCallback != null) {
break;
}
Thread.sleep(1000);
}
if (receivedCallback == null) fail("Callback was not received.");
assertEquals("Expected callback message", receivedCallback);
我正在使用 Cucumber 来测试部署到容器中的服务。该请求包含一个 URL 操作成功时调用的服务。创建等待 Http 回调的 Cucumber 测试的好方法是什么? Gherkin 脚本看起来像这样。
Scenario: Process Order
Given An Order has been submitted
When the Order is processed
Then the order process service calls back with a successful status message
Java 胶水代码是什么样的?
这是我使用嵌入式 http 服务器想出的解决方案。在 OrderSteps.java 胶水代码中,我添加了一个 class 用于在另一个线程上启动服务器。
private static class Callback implements Runnable
{
public void run()
{
HttpServer server;
try
{
server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/callback", new CallbackHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
catch (IOException e)
{
logger.debug("HTTP server loop failure.", e);
}
}
static class CallbackHandler implements HttpHandler
{
@Override
public void handle(HttpExchange t) throws IOException
{
// Read the message and set the global variable
// which informs the main test thread a callback
// has been received.
InputStream is=t.getRequestBody();
byte[] buf=new byte[1000];
int len=is.read(buf);
OrderSteps.receivedCallback=new String(buf,0,len);
String response = "Callback received.";
t.sendResponseHeaders(200, response.length());
// Send response
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
然后在OrderStepsclass中,在发布Order的step/method中,启动服务器线程,然后提交订单。这保证了服务器将收到回调,因为它在订单提交之前正在监听。
// Start a listener for the callback.
Thread callbackThread = new Thread(new Callback());
callbackThread.start();
在检查是否收到回调的 step/method 中,有一个循环检查静态变量以查看它是否已被设置。
// Allow 5 seconds for the callback to occur.
for (int i = 0; i < 5; i++)
{
if (receivedCallback != null) {
break;
}
Thread.sleep(1000);
}
if (receivedCallback == null) fail("Callback was not received.");
assertEquals("Expected callback message", receivedCallback);