有一个名为SessionManagerTest的类。在其中,我按照原样进行模拟,并尝试替换getCurrentSession方法的结果。
private SessionManager sessionManager;
@Before
public void init () {
sessionManager = Mockito.mock (SessionManager.class);
Mockito.when (sessionManager.getCurrentSession (any(), anyInt())). ThenReturn (new MyClassSession (1, 20171111, "cash_user"));
}
方法本身,来自SessionManager类
public MyClassSessionI getCurrentSession (BridgeConfigI bridgeConfig, Integer sessionDay) throws MyException {
MyClassSessionI myClassSession = getSessionFromCache (bridgeConfig.getMandatoryParam ("user"), sessionDay);
if (Optional.ofNullable (myClassSession) .isPresent ()) {
sessionValidator.check (myClassSession);
return myClassSession;
}
myClassSession = getSessionFromDatabase (bridgeConfig, sessionDay);
if (Optional.ofNullable (myClassSession) .isPresent ()) {
sessionValidator.check (vinnicaSession);
addSessionToCache (myClassSession);
}
return myClassSession;
}
但结果不是替代品。并执行getCurrentSession方法。我不明白为什么会这样呢