Mock constructor with mockito

后端 未结 3 934
温柔的废话
温柔的废话 2021-01-18 07:48

I want to mock a constructor into method.

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


        
相关标签:
3条回答
  • 2021-01-18 08:13

    you can send mocked objects as paramemters to your class constructor, form example:

    // define you object
    public MainClassObj instanceClass;
    
    // mock input parameter
    MYClassObj mockedObj = Mockito.mock(MYClassObj.class);
    
    // call construvtor with mocked parameter
    instanceClass = new instanceClass(mockedObj);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-18 08:21

    There are a couple of ways of doing this, described in my article on the Mockito wiki

    0 讨论(0)
提交回复
热议问题