【发布时间】:2012-02-14 13:29:08
【问题描述】:
我有一个AuthenticationManager.authenticate(username,password) 方法,它在被测 SomeService 的 someMethod 中被调用。 AuthenticationManager 被注入到 SomeService 中:
@Component
public class SomeService {
@Inject
private AuthenticationManager authenticationManager;
public void someMethod() {
authenticationManager.authenticate(username, password);
// do more stuff that I want to test
}
}
现在对于单元测试,我需要 authenticate 方法来假装它工作正常,在我的情况下什么都不做,所以我可以测试方法本身是否完成了预期的工作(根据单元测试原则在其他地方测试了身份验证,但是需要在该方法中调用身份验证)所以我在想,我需要SomeService 使用模拟的AuthenticationManager,当authenticate() 被someMethod() 调用时,它只会返回并且什么都不做。
如何使用 PowerMock(或 EasyMock / Mockito,它们是 PowerMock 的一部分)做到这一点?
【问题讨论】:
标签: unit-testing dependency-injection mockito easymock powermock