0

Could you please any one tell me How to mock up Java Constructor ? I tried as below. But it doesn't work.

new MockUp<UserCompanyDivision>()
{ 
    @Mock
    public UserCompanyDivision(String com, String div)
    {
    }    
};
Rogério
  • 16,171
  • 2
  • 50
  • 63
sujan duminda
  • 31
  • 1
  • 6
  • I guess [this](http://code.google.com/p/mockito/wiki/MockingObjectCreation) and [this](https://code.google.com/p/powermock/wiki/MockitoUsage13#How_to_mock_construction_of_new_objects) should help. – Rohit Jain Apr 01 '15 at 05:44

1 Answers1

2

Since constructors don't have names like methods do, JMockit uses the special name "$init" for the corresponding @Mock methods. For example:

new MockUp<UserCompanyDivision>() {
    @Mock // matches a constructor having the same parameters
    void $init(String com, String div) {
        // ...
    }
};

Note that this is described in the API documentation for @Mock.

Rogério
  • 16,171
  • 2
  • 50
  • 63