【发布时间】:2021-02-12 09:40:17
【问题描述】:
想为execute方法中的代码编写junit5测试用例/覆盖如下是我的示例(虚拟)src类,实际方法包含项目大量业务逻辑。
如果我模拟 taskExecutor,它会使用虚拟值绕过整个 execute()。
任何建议我如何在附加代码中覆盖运行方法,下面是示例测试用例
【问题讨论】:
标签: java spring spring-boot junit junit5
想为execute方法中的代码编写junit5测试用例/覆盖如下是我的示例(虚拟)src类,实际方法包含项目大量业务逻辑。
如果我模拟 taskExecutor,它会使用虚拟值绕过整个 execute()。
任何建议我如何在附加代码中覆盖运行方法,下面是示例测试用例
【问题讨论】:
标签: java spring spring-boot junit junit5
您可以将您的内联 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));
【讨论】: