Skip to content

Getting Started

Arthur Zagretdinov edited this page Apr 21, 2019 · 6 revisions

Â# Getting Started #

API's

PowerMock consists of two extension API's.

One for EasyMock and one for Mockito. To use PowerMock you need to depend on one of these API's as well as a test framework.

Currently PowerMock supports JUnit and TestNG. There are three different JUnit test executors available, one for JUnit 4.4-4.12, one for JUnit 4.0-4.3. The test executor for JUnit 3 is not avaliable since PowerMock 2.0.

There's one test executor for TestNG which requires version 5.11+ depending on which version of PowerMock you use.

   Node: Since PowerMock 2.0 Supporting jUnit 3 has been removed.

Writing tests

Write a test like this:

@RunWith(PowerMockRunner.class)
@PrepareForTest( { YourClassWithEgStaticMethod.class })
public class YourTestCase {
...
}

Maven setup

JUnit

TestNG

Non maven users

EasyMock

JUnit: Download the EasyMock zip-file with PowerMock and all its dependencies and add those to your project.

TestNG: Download the EasyMock zip-file with PowerMock and all its dependencies and add those to your project.

Mockito

JUnit: Download the Mockito zip-file with PowerMock and all its dependencies and add those to your project.

TestNG: Download the Mockito zip-file with PowerMock and all its dependencies and add those to your project.

Need to combine PowerMock with another JUnit runner?

Need to bootstrap using a JUnit rule?

Java agent based bootstrapping

Use the PowerMock Java Agent if you're having classloading problems when using PowerMock.

Need Tutorial ?

  1. Clone the project from git using:

Using SSH

git clone git@github.com:powermock/powermock-examples-maven.git

Using HTTPS

git clone https://github.com/powermock/powermock-examples-maven.git
  1. Check out version 1.7.1:
git checkout powermock-1.7.1
  1. Step into the examples/tutorial/ folder and follow the instructions in the readme.txt file.