【发布时间】:2015-04-15 01:55:48
【问题描述】:
考虑到这段代码
class ReportSenderRunnable implements Runnable {
@Override
public void run() {
executeTasks();
}
private void executeTasks() {
try {
runTask1();
} catch (final InterruptedException e) {
logError(ReportStatus.COMPRESSING, e.getMessage());
reportStatus = ReportStatus.EXCEPTION_IN_COMPRESSION;
} catch (final IllegalStateException e) {
logError(ReportStatus.COMPRESSING, e.getMessage());
reportStatus = ReportStatus.EXCEPTION_IN_COMPRESSION;
}
try {
reportStatus = ReportStatus.SENDING;
runTask2();
} catch (final InterruptedException e) {
reportStatus = ReportStatus.EXCEPTION_IN_SENDING;
}
try {
reportStatus = ReportStatus.SUBMITTING_REPORT;
runTask3();
} catch (final InterruptedException e) {
reportStatus = ReportStatus.EXCEPTION_IN_SUBMITTING_REPORT;
}
System.out.println("Report Sender completed");
reportStatus = ReportStatus.DONE;
}
private void logError(final ReportStatus status, final String cause) {
LOGGER.error("{} - {}", status, cause);
}
}
此代码传递给ExecutorService 运行。
private void submitJob() {
final ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new ReportSenderRunnable());
System.out.println("started Report Sender Job");
}
假设runTask1()、runTask2() 和runTask3() 已经在其他地方进行了测试,我该如何测试这段代码?
我很困惑,因为我现在正在学习多线程编程
谢谢
【问题讨论】:
-
new ReportSenderRunnable().run()? -
@Lashane,我正在寻找
JUnit测试多线程代码的方式 -
@daydreamer 我想知道多线程测试是否合适。你要测试的只是
run()中的逻辑,不需要多线程测试
标签: java multithreading concurrency java.util.concurrent