PowerMock
Encyclopedia
PowerMock is a Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 based framework
Software framework
In computer programming, a software framework is an abstraction in which software providing generic functionality can be selectively changed by user code, thus providing application specific software...

 that allows you to unit test code normally regarded as untestable. It is an extension to other mocking
Mock object
In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the...

 frameworks like Mockito
Mockito
Mockito is an open source testing framework for Java released under the MIT License. The framework allows the creation of Test Double objects called, "Mock Objects" in automated unit tests for the purpose of Test-driven Development or Behavior Driven Development .-Distinguishing Features:Mockito...

 or EasyMock. These frameworks are not able to mock certain language features like static methods or constructors. PowerMock enables these frameworks to do so by using a custom classloaders resp. bytecode manipulation.

PowerMock was developed by Johan Haleby und Jan Kronquist. The first major version 1.0 was released in 2008. PowerMock is published using Apache license
Apache License
The Apache License is a copyfree free software license authored by the Apache Software Foundation . The Apache License requires preservation of the copyright notice and disclaimer....

2.0 and can be downloaded at the project homepage.

Example

The following example demonstrates how you can mock static methods with PowerMock. Let’s assume the following setup:
Our unit under test is the class Calculator which just delegates the addition of two integers to MathUtil which offers only static methods:

public class Calculator {

public int add(int a, int b) {
return MathUtil.addInteger(a, b);
}
}

public abstract class MathUtil {

public static final int addInteger(int a, int b) {
return a + b;
}

private MathUtil {}
}

For some reason, we want to mock the MathUtil because in our test scenario the addition should yield other results than it would normally do. The following test shows how to achieve this:


@RunWith(PowerMockRunner.class)
@PrepareForTest( MathUtil.class )
public class CalculatorTest {

/** Unit under test. */
private Calculator calc;

@Before public void setUp {
calc = new Calculator;

PowerMockito.mockStatic(MathUtil.class);
PowerMockito.when(MathUtil.addInteger(1, 1)).thenReturn(0);
PowerMockito.when(MathUtil.addInteger(2, 2)).thenReturn(1);
}

@Test public void shouldCalculateInAStrangeWay {
assertEquals(0, calc.add(1, 1) );
assertEquals(1, calc.add(2, 2) );
}
}


First of all, we use a special test runner provided by the PowerMock framework. With the @PrepareForTest( MathUtil.class ) annotation our class to mock is prepared. This annotation takes a list of all the classes to mock. In our example, this list consists of a single item MathUtil.class.

In our setup method we callPowerMockito.mockStatic(...). (We could have written a static import for the method mockStatic, but that way you can more clearly see where that methods come from.)

Then we define our mock behaiviour calling PowerMockito.when(...). In our tests we have the usual assertions.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK