我有一個AuthenticationManager.authenticate(username,password)
方法,它在SomeService的someMethod中被調用。該AuthenticationManager會被注入到SomeService:如何使用PowerMock/Mockito/EasyMock爲依賴注入使用模擬對象?
@Component
public class SomeService {
@Inject
private AuthenticationManager authenticationManager;
public void someMethod() {
authenticationManager.authenticate(username, password);
// do more stuff that I want to test
}
}
現在的單元測試,我需要在認證方法,只是假裝它工作正常,在我的情況什麼都不做,因此,如果該方法本身並沒有預期的工作,我可以測試(根據單元測試原則在別處測試身份驗證,但是需要在該方法內調用身份驗證)所以我想,我需要SomeService
來使用嘲諷的AuthenticationManager
,它只會返回並在authenticate()
被someMethod()
調用時什麼也不做。
如何使用PowerMock(或EasyMock/Mockito,它是PowerMock的一部分)來做到這一點?
謝謝!真的幫助我徹底改變了我的測試! – Pete 2012-02-15 08:39:24
很酷,我很高興它幫助:) – Brice 2012-02-15 09:02:55