I'm having problem to mock a constructor without parameters while mantaing the constructor with paremeters working as before.
The following is working as expected to me:
Date mockedDate = new Date(1517229382542L);
PowerMockito.whenNew(Date.class).withNoArguments().thenReturn(mockedDate);
System.out.println("without arguments: " + new Date());
But when i try to call the Date constructor with arguments like this:
Date mockedDate = new Date(1517229382542L);
PowerMockito.whenNew(Date.class).withNoArguments().thenReturn(mockedDate);
System.out.println("with arguments: " + new Date(1L));
System.out.println("without arguments: " + new Date());
The constructor with arguments return null.
If i try to mock both constructors like this:
Date mockedDate = new Date(1517229382542L);
PowerMockito.whenNew(Date.class).withArguments(Mockito.anyLong()).thenCallRealMethod();
PowerMockito.whenNew(Date.class).withNoArguments().thenReturn(mockedDate);
System.out.println("with arguments: " + new Date(1L));
System.out.println("without arguments: " + new Date());
I get the following error:
org.mockito.exceptions.base.MockitoException:
Cannot call abstract real method on java object!
Calling real methods is only possible when mocking non abstract method.
//correct example:
when(mockOfConcreteClass.nonAbstractMethod()).thenCallRealMethod();
Can someone help me?
Thanks,
Gustavo Paiva.