Reputation: 119
I want to mock a constructor and return a mock object when the constructor is called. This can be achieved using powermockito's whenNew method like this.
PowerMockito.whenNew(ClassName.class).withAnyArguments().thenReturn(mockObject);
Since Junit5 doesn't have powermockito support yet, I need to know if this can be achieved using Mockito.
Upvotes: 9
Views: 20806
Reputation: 59
Mockito 3.5 has added alot of PowerMock's functionality into core Mockito. It now has a method, mockConstruction(), that you can use to mock constructors. Reference: https://rieckpil.de/mock-java-constructors-and-their-object-creation-with-mockito/
Upvotes: 5