我有方法:
@Override
public void saveCompetence(/*...*/) throws Exception {
try {
operationSupport.mode1(new Runnable() {
@Override
public void run() {
try {
// ......
}
} catch (Exception e) {
LOG.error("Has errors! ", e);
throw new RuntimeException(e);
}
}
});
} catch (Exception e) {
LOG.error("Has errors! ", e);
throw new Exception(Core1.SYSTEM_ERROR);
}
}
我想使用 Mockito 对方法
saveCompetence()
编写单元测试。但是当我调试单元测试时,我不会进入内部 run()
方法。
这是我的方法 mode1():
public void mode1(Runnable callback) {
final SqlSession sqlSession = sqlSessionFactory.openSession();
try {
threadLocalSqlSession.set(sqlSession);
callback.run();
sqlSession.commit();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
sqlSession.rollback(true);
throw new RuntimeException(e);
}
finally {
sqlSession.close();
threadLocalSqlSession.remove();
}
}
调试单元测试时如何进入 run() 方法?
operationSupport
对象是模拟对象吗?如果是这样,它永远不会运行实际的 mode1
方法。我认为你需要使用间谍而不是模拟。
PS:正如@knittl 指出的,这段代码中没有线程。