Mathew Rock Mathew Rock - 1 year ago 106
Java Question

Mock constructor with mockito

I want to mock a constructor into method.

public String generaID() {
GeneraIDParaEntidadCliente aux = new GeneraIDParaEntidadCliente(nombre, registro);

In my test I want do something like this :

when(new GeneraIDParaEntidadCliente(anyString(), any(Entidad.class)).thenReturn(generaIdMock)

but give me this error

Any idea why?

Answer Source

You can use PowerMock to mock constructors.

If you can't use PowerMock for some reason, the most workable solution is to inject a factory to whatever class contains this method. You would then use the factory to create your GeneraIDParaEntidadCliente object and mock the factory.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download