Getting Started

Arthur Zagretdinov edited this page Sep 3, 2017 · 4 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+, one for JUnit 4.0-4.3 and one for JUnit 3 (deprecated).

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 will be dropped

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.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.