Aeron Storm
Aeron Storm

Reputation: 119

Mockito junit 5 mock constructor

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

Answers (1)

truthful-ruth
truthful-ruth

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

Related Questions