Last active
December 20, 2015 16:09
-
-
Save dragester/6159304 to your computer and use it in GitHub Desktop.
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
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); | |
} | |
} |
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
@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