12

Power mock

Embed Size (px)

DESCRIPTION

Relation between Junit, Easy mock and Powermock.

Citation preview

Page 1: Power mock
Page 2: Power mock

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.

Page 3: Power mock

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); }}

Page 4: Power mock

Limitation : Mocking

Page 5: Power mock

Mocking Frameworks

PowerMock

EasyMock

Junit

Page 7: Power mock

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.

Page 8: Power mock

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.

Page 9: Power mock

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.

Page 10: Power mock

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.

Page 11: Power mock

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.

Page 12: Power mock

For more info: [email protected]