How to get mockable current time in java enterprise

With Java version 8 it’s very easy to get the current timestamp with LocalDateTime.now(). One problem that might arise is it’s not easily testable/mockable. Imagine some testing data and conditions depend on the date you set through this call, or you w…


This content originally appeared on DEV Community and was authored by Adrian Matei

With Java version 8 it's very easy to get the current timestamp with LocalDateTime.now(). One problem that might arise is it's not easily testable/mockable. Imagine some testing data and conditions depend on the date you set through this call, or you want to do "time travel". The solution is quite simple - define a service class that produces the current timestamp, which you can then easily mock, in this case we'll name it ProductionCalendar:

@ApplicationScoped
public class ProductionCalendar {
  public LocalDateTime currentTimestamp() {
    return LocalDateTime.now();
  }

  public LocalDate currentDate() {
    return LocalDate.now();
  }

  public String currentTimestamp(Partner partner) {
    return DateTimeFormatter.ofPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS")
        .format(LocalDateTime.now());
  }
}

Then when were you need to call LocalDateTime.now() inject this service and use its methods instead:

@Stateless
public class PartnerRepository {

  @Inject ProductionCalendar productionCalendar;

  @Inject EntityManager entityManager;

  public void savePartner(String partnerNumber) {
    Partner partner = new Partner();
    partner.setPartnerNumber(partnerNumber);
    partner.setCreatedAt(productionCalendar.currentTimestamp());

    entityManager.persist(partner);
  }

  public List<Partner> getPartnerCreatedAfterDate(LocalDateTime timestamp) {
    String queryJpql = "select p from P p where createdAt > :timestamp order by createdAt desc";
    TypedQuery<Partner> query = entityManager.createQuery(queryJpql, Partner.class);
    query.setParameter("timestamp", timestamp);

    return query.getResultList();
  }
}

Then, in the test class you can easily mock ProductionCalendar and calls to its methods and set timestamps as we need them:

@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
public class UserServiceUnitTest {

  @Inject PartnerRepository partnerRepository;

  @Mock ProductionCalendar productionCalendar;

  @Test
  void testGetLatestPartners() {
     when(productionCalendar.currentTimestamp()).thenReturn(LocalDatetime.now().minusDays(1));
     partnerRepository.save("12345");
     var recentPartners = partnerRepository.getPartnerCreatedAfterDate(LocalDatetime.now().minusDays(2));
     assertThat(recentPartners, hasSize(1));
  }

}

Shared with ❤️ from Codever. Use 👉 copy to mine functionality to add it to your personal snippets collection.


This content originally appeared on DEV Community and was authored by Adrian Matei


Print Share Comment Cite Upload Translate Updates
APA

Adrian Matei | Sciencx (2022-02-18T06:30:20+00:00) How to get mockable current time in java enterprise. Retrieved from https://www.scien.cx/2022/02/18/how-to-get-mockable-current-time-in-java-enterprise/

MLA
" » How to get mockable current time in java enterprise." Adrian Matei | Sciencx - Friday February 18, 2022, https://www.scien.cx/2022/02/18/how-to-get-mockable-current-time-in-java-enterprise/
HARVARD
Adrian Matei | Sciencx Friday February 18, 2022 » How to get mockable current time in java enterprise., viewed ,<https://www.scien.cx/2022/02/18/how-to-get-mockable-current-time-in-java-enterprise/>
VANCOUVER
Adrian Matei | Sciencx - » How to get mockable current time in java enterprise. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/18/how-to-get-mockable-current-time-in-java-enterprise/
CHICAGO
" » How to get mockable current time in java enterprise." Adrian Matei | Sciencx - Accessed . https://www.scien.cx/2022/02/18/how-to-get-mockable-current-time-in-java-enterprise/
IEEE
" » How to get mockable current time in java enterprise." Adrian Matei | Sciencx [Online]. Available: https://www.scien.cx/2022/02/18/how-to-get-mockable-current-time-in-java-enterprise/. [Accessed: ]
rf:citation
» How to get mockable current time in java enterprise | Adrian Matei | Sciencx | https://www.scien.cx/2022/02/18/how-to-get-mockable-current-time-in-java-enterprise/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.