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 8530399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:14:33+00:00 2026-06-11T09:14:33+00:00

Does anybody know of a way in powermock you can unit test a constructor

  • 0

Does anybody know of a way in powermock you can unit test a constructor and mock the methods that the constructor calls.

i.e. I have a class like this;

public class myClass {
    public myClass(){
        myMethod();
        // do other stuff
    }
    public void myMethod(){
        // do stuff
    }
}

What I want to do is write a unit test for myClass() constructor that mocks myMethod().

This would be easy if myMethod() was static as I could use mockStaticPartial() then invoke the constructor.

Just creating a partial mock of MyClass won’t work either as once I’ve created the mock I’ve created it, invoking the constructor at that point will just create a new instance of MyClass without myMethod() being mocked.

Anyone know of any ways?

  • 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-06-11T09:14:34+00:00Added an answer on June 11, 2026 at 9:14 am

    You should not mock a class under test. If the constructor calls a method then the actions of the method should be tested as part of testing the constructor. If the method does stuff that is outside the scope of unit testing, mock the classes that method is using to do the “stuff”.

    If you REALLY want to do it with what you have above, (I don’t recommend this) you could create a sub-class of your class under test that overrides the method. (this goes against johncarl’s excellent comment above).

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

Sidebar

Related Questions

Does anybody know of a way to create a DataSet such that when I
Does anybody know a way I can return a user to my app after
does anybody know a way to show that default alert like Your application wants
Does anybody know a way for changing the height of a Telerik Extensions for
Does anybody know a way to make the axis fixed on a CPTXYGraph when
Does anybody know offhand the way to tell whether I'm being called statically (Classname::function)
Does anybody know of a way of generating a boxplot in R with a
Does anybody know of a way to use Guice to inject dependencies into the
Using .NET's Office interop libraries, does anybody know the best way to convert back
Does anybody know if there is a way to display the text value of

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.