Mockito筆記(1) 替換方法行為

Mockito版本 'org.mockito:mockito-core:1.10.19'


import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.testng.annotations.Test;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
/**
* Created by chris_jeng on 2016/1/12.
*/
public class TTest {
class Client {
private Dependency dependency;
public Client(Dependency dependency) {
this.dependency = dependency;
}
public void doSomething() {
dependency.doSomething();
}
}
class Dependency {
public void doSomething() {
System.out.println("Dependency doSomething");
}
}
@Test
public void testMock() {
Dependency dependency = mock(Dependency.class);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
System.out.println("Mcok doSomething");
return null;
}
}).when(dependency).doSomething();
new Client(dependency).doSomething();
}
}


留言