Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6045233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:06:07+00:00 2026-05-23T07:06:07+00:00

Can anyone please summarize, what exactly features gives you adding PowerMock on top of

  • 0

Can anyone please summarize, what exactly features gives you adding PowerMock on top of the Mockito?

So far I’ve found these:

  • mock static, final and private methods
  • remove static initializers
  • allow mocking without dependency injection – this one isn’t clear to me. Can you elaborate?

Does it add anything else? Can you please sum up in several lines?

And do I need to sacrifice something when using PowerMock?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:06:07+00:00Added an answer on May 23, 2026 at 7:06 am

    I don’t know of other benefits offhand, but I want to address 2 of your sub-questions (and this is way too long for a comment):

    allow mocking without dependency injection – this one isn’t clear to me. Can you elaborate?

    I think this came from the Motivation wiki page where they describe a way of refactoring code to not invoke static methods to make it testable. For a concrete example of what I think they’re getting at, let’s say you have this code and you want to test the method mocking the behaviour of the static method, without using powermock:

    public class MyClass {
         public void doGetString() {
             ...
             OtherClass.getString(); //It's complex and scary and needs mocking!
             ...
         }
    }
    

    One solution, would be to pull the static invocation into its own object, then inject an object that can be mocked come test time. For example, without using other frameworks, this could look like:

    public class MyClass {
         public static class StringGetter {
             public getString() {
                 return OtherClass.getString();                 
             }
         }
    
         private final StringGetter getter;
    
         //Existing Constructor
         public MyClass() {
             this(new StringGetter());
         }
    
         //DI Constructor
         MyClass(StringGetter getter) {
             this.getter = getter;
         }
    
         public void doGetString() {
             ...
             getter.getString();
             ...
         }
    }
    

    I’ve seperated the behaviour of my method from the behaviour of the static invocation, and can use the DI constructor to inject mocks easily at test time. Of course with powermock I could just mock the static method in place, and run with it.

    And do I need to sacrifice something when using PowerMock?

    Physically no, but I’d say philosophically yes :). The below are my opinions, and I try to give good reasons behind them, but of course they are opinions so take them with a grain of salt:

    The potentially scary thing that is happening with PowerMock is that in order to accomplish the feats of mocking private and static methods, they are using a custom class loader (which shouldn’t be present at runtime in production) and changing the bytecode of your classes. Arguably, this should not matter with the vast majority of classes most of the time, but if you think about it, if the bytecode has changed, and certain side effects are no longer present, you’re effectively testing different Classes albiet based upon your existing Classes. Yes this is a very academic argument.

    You can somewhat mitigate this first argument by having good comprehensive integration and higher level tests that don’t use PowerMock. In this way you can be more confident in the behaviours of your objects even if your unit tests are using PowerMock.

    The other argument I have against PowerMock, is that it could almost too easily become a crutch. I agree that PowerMock can help with testing code that uses legacy code and other code that you do not have control over. However I would argue that when you have control over the classes that you need to mock, you should avoid its use. If you write a class with a private method or static method that you need to explicitly mock in order to test other methods, my gut instinct would say that this method may be doing too much and should be refactored and broken up. Having PowerMock already available in a project, you may be tempted to just mock it and move on, which would mitigate the pain that should encourage you to refactor the same. Yes there are sometimes due to various technical and non-technical constraints this is not possible, but it’s good to solve pain points instead of avoid them 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone please tell what is change request management tool and what is involved
Can anyone please tell me why compiling a C# application with a .cs file
Can anyone please recommend the best and easiest way to include multiple Javascript files
Can anyone please suggest me a good sample code for zooming image in NSImageView.
Can anyone please tell me when should I use MessageContracts and when should I
Can anyone please explain to me why the following two queries yield different results?
Can anyone please explain me what is DataContractTranslstor and what's it's use?
Can anyone please suggest a way to replace back-slash '\' with slash '/' in
Can anyone please suggest an XPath expression format that returns a string value containing
Can anyone please point out what im doing wrong with this Stored Procedure please.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.