我正在考虑如何使用 mockito 为此编写测试用例。
例如,我的主线程中的部分逻辑是创建一个执行 3 件事的线程。请在下面查看我的注释代码。
现在 RequestThread 可以根据来自主程序的输入数量产生多次。
public class MainThreads {
public static void main(String[] args) {
RequestThread rt = new RequestThread("sample");
rt.start();
//RequestThread another = new RequestThread("sample-2");
//another.start();
//RequestThread newThread = new RequestThread("sample-3");
//newThread.start();
}
public static class RequestThread implements Runnable{
private final String request;
public RequestThread(String request) {
this.request = request;
}
@Override
public void run() {
//1. Instantiate a service passing the required request parameter
MyDataWebService service = new MyDataWebService(request);
//2. Get the returned data
List<String> dataList = service.requestData();
//3. Write to file
Path file = Paths.get("/someDir/" + request);
Files.write(file, dataList, Charset.forName("UTF-8"));
}
}
}
我的问题是,我无法弄清楚如何为线程类正确编写 JUnit/Mockito 测试。我一般不太了解 Mockito 和 JUnit,所以我正在寻找一种方法来对线程应用程序进行单元测试。
有人可以指导我如何对这样的事情进行单元测试吗?
隔江千里
相关分类