Java – mockito: how to test that a constructor is called?

I'm using mockito to test methods in Java applications

How to test that the constructor is called once?

I'm trying a similar verification:

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

But I can't verify that the constructor is called because it doesn't have a method like, for example Do something ()

Solution

This cannot be done with mockito because the object being created is not a mock object This also means that you will not be able to validate anything on the new object

I used to solve this problem by using factory to create objects instead of creating new objects You can then simulate the factory to return the objects required for the test

Whether you are willing to change your design to fit your test depends on you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>