I'm trying to mock the DateTimeFormatter class. I've done the following:
@RunWith(PowerMockRunner.class)
@PrepareForTest({DateTimeFormatter.class})
public class UnitTest {
private DateTimeFormatter mockDateFormatter;
private AwesomeClass awesomeClass;
@Before
public void setUp() {
mockDateFormatter = PowerMockito.mock(DateTimeFormatter.class);
awesomeClass = new AwesomeClass(mockDateFormatter);
}
@Test
public void shouldToTestSomethingAwesome() {
// Other test code
PowerMockito.when(mockDateFormatter.format(any(LocalDate.class)))
.thenReturn("20150224");
// Other test code
}
AwesomeClass
uses it to format LocalDateTime.now(ZoneId.of("UTC"));
. The formatted string is then further used to generate another string. I need to ensure that the string is properly generated. So I need to return a consistent date from either the formatter or mock the LocalDateTime.now(..) static method
What am I doing wrong?