Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock a constructor with parameter

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.

like image 686
Shengjie Avatar asked Nov 13 '12 16:11

Shengjie


People also ask

Can I mock a constructor?

0, we can now mock Java constructors with Mockito. This allows us to return a mock from every object construction for testing purposes. Similar to mocking static method calls with Mockito, we can define the scope of when to return a mock from a Java constructor for a particular Java class.

How do you call a mock method in a constructor?

Faking static methods called in a constructor is possible like any other call. if your constructor is calling a method of its own class, you can fake the call using this API: // Create a mock for class MyClass (Foo is the method called in the constructor) Mock mock = MockManager. Mock<MyClass>(Constructor.

Is it possible to create a mock by calling one of its constructor True or false?

You can, but you probably don't want to: ComplexConstructor partialMock = Mockito. mock(ComplexConstructor. class, CALLS_REAL_METHODS);


1 Answers

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

like image 70
Alban Avatar answered Sep 21 '22 16:09

Alban