---
title: "mock System class with powermock"
author: "jwu"
date: "2013-12-02T14:13:14Z"
tags: []
categories: ["编程语言"]
slug: "mock-system-class-with-powermock"
draft: false
lastmod: "2013-12-02T14:13:14Z"
id: 1983758
url: "https://jwu.iteye.com/blog/1983758"
---

1. Use the `@RunWith(PowerMockRunner.class)` annotation at the class-level of the test case.
2. Use the `@PrepareForTest({ClassThatCallsTheSystemClass.class})` annotation at the class-level of the test case.
3. Use `mockStatic(SystemClass.class)` to mock the system class then setup the expectations as normally.
4. [EasyMock](https://code.google.com/p/powermock/wiki/EasyMock) only: Use `PowerMock.replayAll()` to change to replay mode.
5. [EasyMock](https://code.google.com/p/powermock/wiki/EasyMock) only: Use `PowerMock.verifyAll()` to change to verify mode.

ref: [https://code.google.com/p/powermock/wiki/MockSystem](https://code.google.com/p/powermock/wiki/MockSystem)