2. 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.
3. 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);
}
}
7. 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.
8. 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.
9. 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.
10. 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.
11. 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.