I need to mock a ZonedDateTime.ofInstant() method. I know there are many suggestions in SO but for my specific problem, till now I did not get any simple way out.
Here is my code :
public ZonedDateTime myMethodToTest(){
MyClass myClass;
myClass = fetchSomethingFromDB();
try{
final ZoneId systemDefault = ZoneId.systemDefault();
return ZonedDateTime.ofInstant(myClass.getEndDt().toInstant(), systemDefault);
} catch(DateTimeException dte) {
return null;
}
}
Here is my incomplete Test method :
@Mock
MyClass mockMyClass;
@Test(expected = DateTimeException.class)
public void testmyMethodToTest_Exception() {
String error = "Error while parsing the effective end date";
doThrow(new DateTimeException(error)).when(--need to mock here---);
ZonedDateTime dateTime = mockMyClass.myMethodTotest();
}
I want to mock the ZonedDateTime.ofInstant() method to throw a DateTimeException while parsing for the negative scenario. How I can do that.