---
title: "Throw Errors with Powermock"
url: "https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/throw-errors-with-powermock.md"
canonical: "https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/throw-errors-with-powermock/"
tags: []
author: "Roy Osherove"
description: "Lesson 18 of 21 in The Art of Unit Testing and TDD in Java (Day 3), 1:21. Free video by Roy Osherove."
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Throw Errors with Powermock

1. [Courses](https://courses.osherove.com/#courses)
2. [The Art of Unit Testing and TDD in Java](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/)
3. Day 3

Lesson 18 of 21

Duration: 1:21 [Watch on YouTube](https://www.youtube.com/watch?v=1T04JPawUiU)

A quick demo showing how to make static methods throw errors with Powermock.

## In this lesson

Roy Osherove shows how to make the write method of a static logger throw an exception with [PowerMock](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-6#ch06lev1sec1 "Constrained and unconstrained frameworks (The Art of Unit Testing, 2nd ed., ch. 6)"): after mock static, doThrow with an out of memory exception is set on the static logger class, and the next call to the static logger is the one that throws. He notes that this API is not obvious at first and that a matcher such as any string tells the reader more.

## Transcript

196 words

[\[00:03\]](https://www.youtube.com/watch?v=1T04JPawUiU&t=3s) I didn't show how to throw how to throw expect exceptions with the [PowerMock](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-6#ch06lev1sec1 "Constrained and unconstrained frameworks (The Art of Unit Testing, 2nd ed., ch. 6)"). So this is a very simple demo for that. Now it looks weird the first time you do it but if I wanted the write method of static logger to throw an exception I would have to do this. I do the mock static just like before. I say do throw new out of memory exception when then I say static logger.class class not the method that's the limitations of the API and then the next call to the static logger is the thing that will throw the exception

[\[00:42\]](https://www.youtube.com/watch?v=1T04JPawUiU&t=42s) Okay so it's better to use a matcher like any string etc so that the reader will know a bit more that this is a thing or maybe it's better to do something like this maybe that's a bit more readable I'm not sure Maybe this way. Either way, I wanted to show that this thing is somehow connected to the things above. Okay? So, it's not very obvious when you just start to use the API.

[\[01:14\]](https://www.youtube.com/watch?v=1T04JPawUiU&t=74s) So, you should know how to do that. How to simulate behavior from something static. Okay?

## Related

- [Simulating fake values](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-5#ch05lev1sec3) · The Art of Unit Testing, 2nd ed., ch. 5

    The 2nd edition's section on simulating fake values with a framework, including making a fake throw an exception.

- [Faking static methods in Moq, FakeItEasy and NSubstitute might be closer than imagined](https://osherove.com/blog/2012/7/8/faking-static-methods-in-moq-fakeiteasy-and-nsubstitute-migh.html) · osherove.com

    Roy's post on faking static methods with constrained .NET frameworks, the same static-faking problem.

### Also linked in this lesson

#### The Art of Unit Testing, 2nd ed.

- [Constrained and unconstrained frameworks](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-6#ch06lev1sec1), ch. 6

[Previous lesson Unconstrained Isolation Frameworks](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/unconstrained-isolation-frameworks/) [Next lesson Discussion About Pros and Cons of Unconstrained Frameworks](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/discussion-about-pros-and-cons-of-unconstrained-frameworks/)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
