Mock Instant.now() без использования Clock в конструкторе или без объекта Clock ⇐ JAVA
Mock Instant.now() без использования Clock в конструкторе или без объекта Clock
I have below code in one of my methods
ZonedDateTime current = Instant.now().atZone(ZoneId.of(AMERICA_NEW_YORK)); I want to mock current in the JUnit test.
I tried with java.time.Clock but for this, I need to add it into the class constructor as my code is written into old versions of Spring and using XML based configuration this class cause issue because it requires constructor argument in the application-context.xml file if I use a constructor with Clock.
Is there any way to avoid constructor configuration and mock current in the above code.
Update
As per Pavel Smirnov's comments, I tried below but current still returning today's date but not the one which I am mocking.
ZonedDateTime exactOneDay = ZonedDateTime.parse("Sun Oct 21 12:30:00 EDT 2018", Parser); doReturn(exactOneDay).when(spyEmployeeHelper).getCurrentTime(); employee = getEmployees().get(0); assertEquals(Integer.valueOf(1), employee.getNoticePeriod());
Источник: https://stackoverflow.com/questions/552 ... ock-object
I have below code in one of my methods
ZonedDateTime current = Instant.now().atZone(ZoneId.of(AMERICA_NEW_YORK)); I want to mock current in the JUnit test.
I tried with java.time.Clock but for this, I need to add it into the class constructor as my code is written into old versions of Spring and using XML based configuration this class cause issue because it requires constructor argument in the application-context.xml file if I use a constructor with Clock.
Is there any way to avoid constructor configuration and mock current in the above code.
Update
As per Pavel Smirnov's comments, I tried below but current still returning today's date but not the one which I am mocking.
ZonedDateTime exactOneDay = ZonedDateTime.parse("Sun Oct 21 12:30:00 EDT 2018", Parser); doReturn(exactOneDay).when(spyEmployeeHelper).getCurrentTime(); employee = getEmployees().get(0); assertEquals(Integer.valueOf(1), employee.getNoticePeriod());
Источник: https://stackoverflow.com/questions/552 ... ock-object
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение