7

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.

Aeron Storm
  • 99
  • 1
  • 1
  • 11
  • 1
    I don't think so ```Junit5 doesn't have powermockito support yet``` you need to add an external lib. Possibly duplicate https://stackoverflow.com/questions/13364406/mock-a-constructor-with-parameter – prostý člověk Nov 19 '20 at 09:22

1 Answers1

5

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/

truthful-ruth
  • 51
  • 1
  • 2