【发布时间】:2018-07-12 04:53:29
【问题描述】:
我有一个主程序,它创建一个线程来做某事。
工作线程调用一个长时间运行的服务并使用某种类型 回调函数以通知其响应。
public class TestingThreads {
public static void main(String[] args) {
Thread workerThread = new Thread(new WorkerThread());
workerThread.run();
}
public static class WorkerThread implements Runnable {
private ConfigService service;
@Override
public void run() {
Response response = new Response() {
@Override
public void onResponse(String result) {
//Do something on the response
}
};
Request request = new Request();
service.callSomething(request, response);
//Wait for response before exiting this run loop
}
}
public static abstract class Response {
public abstract void onResponse(String result);
}
public static class Request {
}
public static class ConfigService {
public void callSomething(Request request, Response response) {
// Call Long Running Process
}
}
}
我在为此用例创建 JUnit 测试用例时遇到问题。 您知道如何为此类执行单元测试吗?
我想实际模拟我的回复,但我不知道该怎么做。 在我的单元测试中,我不想真正调用我的 ConfigService 类。
【问题讨论】:
-
你的意思是你必须等待很长时间才能得到响应来验证它?
-
是的......当单元测试时......你实际上并没有正确调用服务而是使用一些模型?
-
如果这是你想要的,你可以使用 Thread t = new WorkerThread(); t.join();
-
请发布更多代码(如何发送请求?)
标签: java unit-testing junit