如何对 Runnable Class 进行单元测试?

How do I Unit Test Runnable Class?

考虑这段代码

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() 已经在别处测试过,我该如何测试这段代码?

我很迷茫,因为我现在正在学习多线程编程

谢谢

你可以试试这样测试

public class TestMultiThread {
@Test
public void testThread(){
    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.execute(new ReportSenderRunnable());
    System.out.println("started Report Sender Job");
}
}