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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:16:14+00:00 2026-06-09T18:16:14+00:00

Description: I cannot seem to have my stubs or mocks take affect in the

  • 0

Description:

I cannot seem to have my stubs or mocks take affect in the class I have under test. I am trying to use the whenNew action so I can mock a return object and then mock a operation on that object with a returned value.

I imagine its something simple I am missing but not seeing it.

SOLUTION: Originally I was running with MockitoRunner.class and it required being changed to PowerMockRunner.class. Code below reflects the solution.

Jars on the classpath:

  • powermock-mockito-1.4.11-full.jar
  • mockoito-all-1.9.0.jar
  • javassist-3.15.0-GA.jar
  • junit-4.8.2.jaf
  • objensis-1.2.jar
  • cglib-nodep-2.2.2.jar

TEST CLASS

   import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.mockito.PowerMockito;
    import static org.powermock.api.mockito.PowerMockito.*;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    import static org.mockito.Matchers.any;
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(ClassA.class)
    public class ClassATest {

        @Test
        public void test() throws Exception
        {
                String[] returnSomeValue = {"PowerMockTest"};
                String[] inputValue = {"Test1"};
                ClassB mockedClassB = mock(ClassB.class);
                whenNew( ClassB.class).withNoArguments().thenReturn( mockedClassB );
                when( mockedClassB, "getResult", any(String[].class) ).thenReturn(returnSomeValue);       

                IClassA classUnderTest = new ClassA();
                String[] expectedValue = classUnderTest.runTest(inputValue);      
        }

    }

Class A Implementation

public class ClassA implements IClassA {

    @Override
    public String[] runTest(String[] inputValues) {

        String[] result;
        IClassB classB = new ClassB();
        result = classB.getResult(inputValues);

        return result;
    }

} 
  • 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-09T18:16:16+00:00Added an answer on June 9, 2026 at 6:16 pm

    Since you are using powermock features (@PrepareForTest, PowerMockito.whenNew etc.), you have to run your test with the PowerMockRunner.

    @RunWith(PowerMockRunner.class)
    

    Because ClassB#geResult is not private, you may also simplify your code and replace

    when( mockedClassB, "getResult", any(String[].class) ).thenReturn(someValue); 
    

    by

    when(mockedClassB.getResult(any(String[].class))).thenReturn(someValue);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just cannot seem to get the tree grid up and running. I have
I have a JSON response returning from an Ajax call but cannot seem to
I have a problem that I cannot seem to find a solution for. I
Description: If we use java objects jruby get permgen too: System.out.println(Initialazing..); //Spring applicaton context
Description: I have a problem regarding DataGridView . I need to show a Client_Name
I have made a personality test users can fill in. (Its in dutch, find
I am trying to implement drag and drop between two listboxes. I have a
Perhaps there is an easy solution for my problem but I simply cannot seem
I cannot seem to get hold of the parent varible from the child template,
I can seem to fix this little bug I have. I have a dynamic

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.