繁体   English   中英

如何使用 junit5 覆盖在 taskExecutor.execute(new Runnable..) 中编写的代码

[英]How to Cover code written inside taskExecutor.execute(new Runnable..) using junit5

想要为 execute 方法中的代码编写 junit5 测试用例/覆盖率,如下是我的示例(虚拟)src 类,实际方法包含项目大量业务逻辑。

在此处输入图片说明

如果我模拟 taskExecutor,它会用虚拟值绕过整个 execute()。

任何建议我如何在附加代码中覆盖运行方法,以下是示例测试用例

在此处输入图片说明

您可以将内联的Runnable外包给它自己的类,例如:

public class MyRunnable implements Runnable {

  private final JdbcTemplate jdbcTemplate;

  public MyRunnable(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }

  @Override
  public void run() {
    // ... do your logic here
  }
}

然后为这个类编写一个单元测试来单独验证它的行为。

在你的RedisScheduler你可以使用你的新类:

taskExecutor.execute(new MyRunnable(jdbcTemplate));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM