Power mock

1,244 views
990 views

Published on

Relation between Junit, Easy mock and Powermock.

Published in: Engineering
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,244
On SlideShare
0
From Embeds
0
Number of Embeds
149
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Power mock

  1. 1. JunitOverview Creating a test class in Junit: • Define a subclass of TestCase • Override the setUp() method to initialize object(s) under test. • Override the tearDown() method to release object(s) under test. • Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results. • Define a static suite() factory method that creates a TestSuite containing all the testXXX() methods of the TestCase. • Optionally define a main() method that runs the TestCase in batch mode.
  2. 2. Example public class CounterTest extends junit.framework.TestCase { Counter counter1; public CounterTest() { } // default constructor protected void setUp() { // creates a (simple) test fixture counter1 = new Counter(); } protected void tearDown() { } // no resources to release public void testIncrement() { assertTrue(counter1.increment() == 1); assertTrue(counter1.increment() == 2); } public void testDecrement() { assertTrue(counter1.decrement() == -1); } }
  3. 3. Limitation : Mocking
  4. 4. Mocking Frameworks PowerMock EasyMock Junit
  5. 5. EasyMock Quick Review
  6. 6. PowerMock • PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. • PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.
  7. 7. Mocking private methods • Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case. • Use the @PrepareForTest(ClassWithPrivateMethod.class) annotation at the class- level of the test case. • Use PowerMock.createPartialMock(ClassWithPrivateMethod.class, "nameOfTheMethodToMock") to create a mock object that onlymocks the method with name nameOfTheMethodToMock in this class (let's call it mockObject). • Use PowerMock.expectPrivate(mockObject, "nameOfTheMethodToMock", argument1, argument2) to expect the method call tonameOfTheMethodToMock with arguments argument1 and argument2. • Use PowerMock.replay(mockObject) to change the mock object to replay mode. • Use PowerMock.verify(mockObject) to change the mock object to verify mode.
  8. 8. Mocking static methods • Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case. • Use the @PrepareForTest(ClassThatContainsStaticMethod.class) annotation at the class-level of the test case. • Use PowerMock.mockStatic(ClassThatContainsStaticMethod.class) to mock all methods of this class. • Use PowerMock.replay(ClassThatContainsStaticMethod.class) to change the class to replay mode. • Use PowerMock.verify(ClassThatContainsStaticMethod.class) to change the class to verify mode.
  9. 9. MockFinal • Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case. • Use the @PrepareForTest(ClassWithFinal.class) annotation at the class-level of the test case. • Use PowerMock.createMock(ClassWithFinal.class) to create a mock object for all methods of this class (let's call it mockObject). • Use PowerMock.replay(mockObject) to change the mock object to replay mode. • Use PowerMock.verify(mockObject) to change the mock object to verify mode.
  10. 10. Mocking static methods • Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case. • Use the @PrepareForTest(ClassThatContainsStaticMethod.class) annotation at the class-level of the test case. • Use PowerMock.mockStatic(ClassThatContainsStaticMethod.class) to mock all methods of this class. • Use PowerMock.replay(ClassThatContainsStaticMethod.class) to change the class to replay mode. • Use PowerMock.verify(ClassThatContainsStaticMethod.class) to change the class to verify mode.
  11. 11. For more info: m@piyushmittal.com

×