Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 547
Aeron Storm Avatar asked May 28 '26 23:05

Aeron Storm


1 Answers

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/

like image 191
truthful-ruth Avatar answered May 31 '26 14:05

truthful-ruth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!