Mockto: how to verify that a constructor has been called?

I use Mockitoto test methods in my application Java.

How can I verify that the constructor has been called?

I am trying to make a confirmation like this:

verify(myclass, times(1)).doSomething(anotherObject);

But I can’t check if the constructor was called, because it doesn’t have a method similar to, for example,

+4
source share
6 answers

This cannot be done with Mockito, because the object being created is not a mocked object. It also means that you cannot check anything at this new facility.

, Factory , . Factory, , .

, !

+8

Mockito PowerMockito.

, ClassUnderTest

public class ClassUnderTest {
    String name;
    boolean condition;

    public ClassUnderTest(String name, boolean condition) {
       this.name = name;
       this.condition = condition;
       init();
    }

    ...
}

,

public class MyClass {

    public MyClass() { } 

    public void createCUTInstance() {
       // ...
       ClassUnderTest cut = new ClassUnderTest("abc", true);
       // ...
    }

    ...
}

Test ...

(1) PowerMockRunner PrepareForTest:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ClassUnderTest.class, MyClass.class })
public class TestClass {

(2) , :

@Before
public void setup() {
    ClassUnderTest cutMock = Mockito.mock(ClassUnderTest.class);
    PowerMockito.whenNew(ClassUnderTest.class)
                .withArguments(Matchers.anyString(), Matchers.anyBoolean())
                .thenReturn(cutMock);
}

(3) :

@Test
public void testMethod() {
    // prepare
    MyClasss myClass = new MyClass();

    // execute
    myClass.createCUTInstance();

    // checks if the constructor has been called once and with the expected argument values:
    String name = "abc";
    String condition = true;
    PowerMockito.verifyNew(ClassUnderTest.class).withArguments(name, condition);
}
+8

verify() ( ). constructor created object.

+1

Mockito .

factory . factory , create.

, , , unit test. (DI) (IoC).

+1

, ... , , , .

, , :

Object obj = null;

obj = new Object();

if (obj == null) {
  //... Didn't Work
} else {
  //... Worked
}
0

you won’t be able to mock the constructor with mockito, but using powermockito, you should be able to and then test it. The setting will be something like this.

public class MyClass{

   public MyClass(String name){}

   public void doSomethingUseFul(){
      //.......
   }

}

public class Helper {

   public void goHelp(){
       new MyClass("apple").doSomethingUseFul();
   } 

}

// mock

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(MyClass.class)
    public class MockMyClass {
    @InjectMocks
    private Helper helper;

        @Test
        public void testDoSomethingUseFul() {
             MyClass  c = mock(MyClass.class); 
             doNothing().when(c).doSomethingUseFul();
             PowerMockito.whenNew(MyClass. class).withArguments(anyString()).thenReturn(c);
             helper.goHelp(); 
             verifyNew(MyClass.class).withArguments(anyString())


        }
    }
0
source

Source: https://habr.com/ru/post/1650645/


All Articles