Skip to content

Instantly share code, notes, and snippets.

@dragester
Last active December 20, 2015 16:09
Show Gist options
  • Save dragester/6159304 to your computer and use it in GitHub Desktop.
Save dragester/6159304 to your computer and use it in GitHub Desktop.
public class PowerMockDemo {
public void someMethod(String someArgument){
ClassA a = new ClassA();
ClassB b = new ClassB();
ClassC c = new ClassC();
c.setFirst(a);
c.setSecond(b);
c.execute(someArgument);
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(PowerMockDemo.class)
public class PowerMockDemoTest {
private PowerMockDemo toTest;
@Test
public void testExcuteMethod() throws Exception {
toTest = new PowerMockDemo();
// creates a new mock of ClassA
ClassA mockA = createMock(ClassA.class);
expectNew(ClassA.class).andReturn(mockA);
// creates a new mock of ClassB
ClassB mockB = createMock(ClassB.class);
expectNew(ClassB.class).andReturn(mockB);
// creates a new "strict" mock of ClassC
ClassC mockC = createStrictMock(ClassC.class);
expectNew(ClassC.class).andReturn(mockC);
mockC.setFirst(mockA);
expectLastCall().once();
mockC.setSecond(mockB);
expectLastCall().once();
mockC.execute(EasyMock.anyObject(String.class));
expectLastCall().once();
replayAll();
// run the test code
toTest.someMethod("Hello PowerMock");
verifyAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment