Mock constructor
public class Example{
public Example(){
System.out.println("Default construsture");
}
public Example(int a){
System.out.println("Construsture: " + a);
}
}
@Test
public void test() throws Exception {
Example example = PowerMockito.mock(Example.class);
PowerMockito.whenNew(Example.class).withNoArguments().thenReturn(example);
Example example1 = new Example();
assertEquals(example, example1);
PowerMockito.whenNew(Example.class).withAnyArguments().thenReturn(example);
Example example2 = new Example(1);
assertEquals(example, example2);
PowerMockito.whenNew(Example.class).withArguments(Matchers.anyInt()).thenReturn(example);
Example example3 = new Example(1);
assertEquals(example, example3);
}
Partial Any
public class Example{
public Example(){
}
public void setData(String key, String aValue){
// ...
}
}
@Test
public void test() throws Exception {
Example example = PowerMockito.mock(Example.class);
PowerMockito.doNothing().when(example).setData(Matchers.same("test"), Matchers.anyString());
example.setData("test", "123");
Mockito.verify(example, Mockito.timeout(1)).setData(Matchers.same("test"), Matchers.anyString());
}
留言
張貼留言