Mockito版本 'org.mockito:mockito-core:1.10.19'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
留言
張貼留言
有什麼想法歡迎跟我們分享