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

  • Home
  • SEARCH
  • 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 8223035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:31:48+00:00 2026-06-07T14:31:48+00:00

I am mocking a static method as follows: class A{ static String methodA(HashMap<String,Boolean> h){

  • 0

I am mocking a static method as follows:

class A{
    static String methodA(HashMap<String,Boolean> h){
        ...  
        Set<String> keys=h.entrySet();  
    }
}

Powermockito code:

Powermockito.mockstatic(A.class);  
when(A.methodA(any(HashMap.class)).thenReturn("Hey");  

Now, when I do not expect a call to methodA to enter the function definiton, but directly return “hey”. However, this is not happening. Despite the mock, the call to methodA() enters it, and since I am passing any(HashMap.class), a null value is passed. I get a NullPointerException. What am I doing wrong?

  • 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-07T14:31:50+00:00Added an answer on June 7, 2026 at 2:31 pm

    You need to have following on top of your class:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({ A.class})
     class ATest {
     ....
    }
    

    The annotation @RunWith, indicates that PowerMockRunner is to be used for executing the test case. Any class which requires static or private methods to be mocked, goes into @PrepareForTest, in this case : class A .

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

Sidebar

Related Questions

What is the best approach for mocking out a static class in c#. For
I am mocking in this sort of situation: class A { public IB B
I'm having hard time mocking the save instance method in my unit tests in
Can someone please help? I use Google code’s Moq framework for mocking within my
I'm new to mocking, I have a method which takes a lambda as a
I'm attempting to unit test some code using NUnit. I have a method: public
I am using mockito as mocking framework. I have a scenerio here, my when(abc.method()).thenReturn(value)
If I'm mocking a class, like below, is there any way I can get
Let's say I have a utility class DateUtil (see below). To use this method
I'm mocking the WebOperationContext class over wrapper for unit testing (using Moq). But I

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.