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

The Archive Base Latest Questions

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

I have a class as below: public class A { public A(String test) {

  • 0

I have a class as below:

public class A {
    public A(String test) {
        bla bla bla
    }

    public String check() {
        bla bla bla
    }
}

The logic in the constructor A(String test) and check() are the things I am trying to mock. I want any calls like: new A($$$any string$$$).check() returns a dummy string "test".

I tried:

 A a = mock(A.class); 
 when(a.check()).thenReturn("test");

 String test = a.check(); // to this point, everything works. test shows as "tests"

 whenNew(A.class).withArguments(Matchers.anyString()).thenReturn(rk);
 // also tried:
 //whenNew(A.class).withParameterTypes(String.class).withArguments(Matchers.anyString()).thenReturn(rk);

 new A("random string").check();  // this doesn't work

But it doesn’t seem to be working. new A($$$any string$$$).check() is still going through the constructor logic instead of fetch the mocked object of A.

  • 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-14T17:48:34+00:00Added an answer on June 14, 2026 at 5:48 pm

    The code you posted works for me with the latest version of Mockito and Powermockito. Maybe you haven’t prepared A?
    Try this:

    A.java

    public class A {
         private final String test;
    
        public A(String test) {
            this.test = test;
        }
    
        public String check() {
            return "checked " + this.test;
        }
    }
    

    MockA.java

    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.Matchers.equalTo;
    import static org.mockito.Mockito.mock;
    import static org.mockito.Mockito.when;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(A.class)
    public class MockA {
        @Test
        public void test_not_mocked() throws Throwable {
            assertThat(new A("random string").check(), equalTo("checked random string"));
        }
        @Test
        public void test_mocked() throws Throwable {
             A a = mock(A.class); 
             when(a.check()).thenReturn("test");
             PowerMockito.whenNew(A.class).withArguments(Mockito.anyString()).thenReturn(a);
             assertThat(new A("random string").check(), equalTo("test"));
        }
    }
    

    Both tests should pass with mockito 1.9.0, powermockito 1.4.12 and junit 4.8.2

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

Sidebar

Related Questions

I have a test like below: public class TestSizeAnnotation { public static void main(String[]
Does making the below class final (adding public final class) have any real impact
I have a structure like below: public class BaseClass { public string SendError(string Message){
I have a class which extends a JPanel. Code below: public class Test extends
I am trying to test a class similar to the example below: public class
I have a test class and a class like below. public class Foo {
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have a class that implements InvocationHandler as below: public class MyProxyClass implements InvocationHandler
Say I have a class with three constructors, like the one below: public class
I have a Student class in my application as below: public class Student {

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.