PowerMock

from Wikipedia, the free encyclopedia
PowerMock

PowerMock logo
Basic data

Maintainer Johan Haleby, Jan Kronquist
Current  version 2.0.7
( March 30, 2020 )
operating system Cross-platform
programming language Java
category Test - Framework
License Apache license
powermock.org

PowerMock is a Java - Framework , to create mock objects for unit testing . It runs as an extension of other mocking frameworks such as Mockito or Easymock and extends their range of functions with the ability to mock certain language aspects such as static methods or constructors .

PowerMock uses techniques such as its own class loader or bytecode manipulation in order to manipulate aspects in the mock objects that cannot be changed with the usual language constructs, such as static or final methods or constructors.

PowerMock was originally developed by Johan Haleby and Jan Kronquist, now more than 10 developers are working on the software. In 2008 the first major release appeared with version 1.0. PowerMock is subject to the Apache 2.0 license . Powermock is used for testing by well-known frameworks and tools such as Spring , Neo4j , Checkstyle , JBoss AS or Hudson .

example

The following example demonstrates some of the possibilities that PowerMock offers. The following scenario is given:

Test scenario

The unit to be tested is the class Calculatorthat MathUtildelegates the calculation of two integer values ​​to the class that only offers 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() {}
}

The behavior of MathUtilshould now be mocked, because in the test scenario the addition should deliver different results than usual. The following test enables 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, a special test runner is used which is provided by the PowerMock framework. The class to be mocked is prepared with the annotation @PrepareForTest( MathUtil.class ) . This annotation can also process a whole list of classes to be mocked. In our example the list consists of one element MathUtil.class.

In the setup method is PowerMockito.mockStatic(...)called.

The mock behavior of the static method is now defined by calling PowerMockito.when(...). Then come the typical assertions in the actual tests .

See also

literature

  • Michael Tamm: JUnit-Profiwissen: Efficient working with the standard library for automated tests in Java . dpunkt.verlag, 2013, ISBN 978-3-86491-410-2 , 6.9 PowerMock ( google.de [accessed on January 20, 2018]).

Web links

Individual evidence

  1. PowerMock 2.0.7 Final . March 30, 2020 (accessed June 29, 2020).
  2. The PowerMock Open Source Project on Open Hub: Languages Page . In: Open Hub . (accessed on September 6, 2018).
  3. PowerMock Homepage (English)
  4. Powermock facts on OpenHub
  5. Powermock-JUnit4 user
  6. Blog: Testing and Mocking Static Methods in Java