111

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
Shengjie
  • 12,336
  • 29
  • 98
  • 139

7 Answers7

104

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

Alban
  • 1,915
  • 1
  • 14
  • 17
55

To my knowledge, you can't mock constructors with mockito, only methods. But according to the wiki on the Mockito google code page there is a way to mock the constructor behavior by creating a method in your class which return a new instance of that class. then you can mock out that method. Below is an excerpt directly from the Mockito wiki:

Pattern 1 - using one-line methods for object creation

To use pattern 1 (testing a class called MyClass), you would replace a call like

   Foo foo = new Foo( a, b, c );

with

   Foo foo = makeFoo( a, b, c );

and write a one-line method

   Foo makeFoo( A a, B b, C c ) { 
        return new Foo( a, b, c );
   }

It's important that you don't include any logic in the method; just the one line that creates the object. The reason for this is that the method itself is never going to be unit tested.

When you come to test the class, the object that you test will actually be a Mockito spy, with this method overridden, to return a mock. What you're testing is therefore not the class itself, but a very slightly modified version of it.

Your test class might contain members like

  @Mock private Foo mockFoo;
  private MyClass toTest = spy(new MyClass());

Lastly, inside your test method you mock out the call to makeFoo with a line like

  doReturn( mockFoo )
      .when( toTest )
      .makeFoo( any( A.class ), any( B.class ), any( C.class ));

You can use matchers that are more specific than any() if you want to check the arguments that are passed to the constructor.

If you're just wanting to return a mocked object of your class I think this should work for you. In any case you can read more about mocking object creation here:

http://code.google.com/p/mockito/wiki/MockingObjectCreation

Ben Glasser
  • 3,216
  • 3
  • 24
  • 41
  • 26
    +1, I don't like the fact that I need to adjust my source code make it more mockito friendly. Thanks for the sharing. – Shengjie Nov 13 '12 at 17:16
  • 24
    It's never bad to have source code that is more testable, or to avoid testability anti-patterns when you write your code. If you write source that is more testable, it's automatically more maintainable. Isolating your constructor calls in their own methods is just one way to achieve this. – Dawood ibn Kareem Nov 18 '12 at 03:23
  • 4
    Writing testable code is good. Being forced to redesign class A so that I can write tests for class B, which depends on A, because A has a hard-coded dependency on C, feels...less good. Yeah, the code will be better in the end, but how many classes will I end up redesigning so that I can finish writing one test? – Mark Wood Aug 06 '20 at 17:20
  • @MarkWood in my experience clunky testing experiences are generally a sign of some design flaw. IRL if you are testing constructors your code is probably screaming at you for a factory or some dependency injection. If you follow typical design patterns for those two cases your code becomes much easier to test and to work with in general. If you are testing constructors because you have bunch of logic in there, you probably need some layer of polymorphism, or you could move that logic to an initialization method. – Ben Glasser Aug 07 '20 at 18:35
  • Working solution example with Spy: @Mock private PickCreationUtil pickCreationUtil; // inside constructor pickCreationUtil = spy(new PickCreationUtil(echoLogger)); – Waqas Ahmed Sep 23 '22 at 02:18
  • With Mockito 3.5, you can now mock constructors. Check https://javadoc.io/doc/org.mockito/mockito-core/3.5.0/org/mockito/Mockito.html#49 – zafar142003 Apr 16 '23 at 11:57
45

With Mockito you can use withSettings(). For example if the CounterService required 2 dependencies, you can pass them as a mock:

 UserService userService = Mockito.mock(UserService.class);
 SearchService searchService = Mockito.mock(SearchService.class);   
 CounterService counterService = Mockito.mock(CounterService.class, withSettings().useConstructor(userService, searchService));
MevlütÖzdemir
  • 3,180
  • 1
  • 23
  • 28
16

Starting with version 3.5.0 of Mockito and using the InlineMockMaker, you can now mock object constructions:

 try (MockedConstruction mocked = mockConstruction(A.class)) {
   A a = new A();
   when(a.check()).thenReturn("bar");
 }

Inside the try-with-resources construct all object constructions are returning a mock.

rieckpil
  • 10,470
  • 3
  • 32
  • 56
  • 1
    How does this differ from `A a = mock(A.class)`? Also, I don't think this answered the question about parameters – OneCricketeer Dec 08 '21 at 21:37
  • Your answer is useful, however it's missing the part where you can verify the passed parameters, see my answer. – apetrelli Dec 28 '22 at 15:20
  • @OneCricketeer the difference is that the constructor may be called inside the class you are testing. If you cannot pass to the tested code the constructed object, mocking outside of it is of no use. – apetrelli Dec 28 '22 at 15:23
13

Without Using Powermock .... See the example below based on Ben Glasser answer since it took me some time to figure it out ..hope that saves some times ...

Original Class :

public class AClazz {

    public void updateObject(CClazz cClazzObj) {
        log.debug("Bundler set.");
        cClazzObj.setBundler(new BClazz(cClazzObj, 10));
    } 
}

Modified Class :

@Slf4j
public class AClazz {

    public void updateObject(CClazz cClazzObj) {
        log.debug("Bundler set.");
        cClazzObj.setBundler(getBObject(cClazzObj, 10));
    }

    protected BClazz getBObject(CClazz cClazzObj, int i) {
        return new BClazz(cClazzObj, 10);
    }
 }

Test Class

public class AClazzTest {

    @InjectMocks
    @Spy
    private AClazz aClazzObj;

    @Mock
    private CClazz cClazzObj;

    @Mock
    private BClazz bClassObj;

    @Before
    public void setUp() throws Exception {
        Mockito.doReturn(bClassObj)
               .when(aClazzObj)
               .getBObject(Mockito.eq(cClazzObj), Mockito.anyInt());
    }

    @Test
    public void testConfigStrategy() {
        aClazzObj.updateObject(cClazzObj);

        Mockito.verify(cClazzObj, Mockito.times(1)).setBundler(bClassObj);
    }
}
Raphael Amoedo
  • 4,233
  • 4
  • 28
  • 37
user666
  • 1,104
  • 12
  • 20
4

Mockito has limitations testing final, static, and private methods.

Alternate Solution: with jMockit testing library, you can do few stuff very easy and straight-forward as below:

Mock constructor of a java.io.File class:

new MockUp<File>(){
    @Mock
    public void $init(String pathname){
        System.out.println(pathname);
        // or do whatever you want
    }
};
  • the public constructor name should be replaced with $init
  • arguments and exceptions thrown remains same
  • return type should be defined as void

Mock a static method:

  • remove static from the method mock signature
  • method signature remains same otherwise
Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38
3

Using Mockito 4 (but I suspect this is true for Mockito from 3.5.0 version) you can mock the constructor and, in the initializer, you can assert the values of the parameters.

For example:

try (MockedConstruction<A> constr = mockConstruction(A.class,
            (mock, context) -> {
                if (context.getCount() == 1) {
                    assertArrayEquals(context.arguments().toArray(), new Object[] {"test"});
                } else {
                    fail("No more calls should happen");
                }
            })) {
        // Do the rest of assertions.
    }

Notice that you need to put the MockedConstruction instantiation in a try-with-resources otherwise the mocked construction is leaked outside the test.

apetrelli
  • 718
  • 4
  • 18