javatestingmockingjodatime

How can I mock JodaTime actual date?


I want to test this method:

 public FirmOrder findActiveByModelColor(ModelColor modelColor) {
   Query query = em.createQuery("FROM FirmOrder fo WHERE fo.modelColor = :modelColor AND fo.year = :year AND fo.month = :month");
   query.setParameter("modelColor", modelColor);
   query.setParameter("year", new DateTime().year().get());
   query.setParameter("month", new DateTime().monthOfYear().get());
   return (FirmOrder) query.getSingleResult();
 }

but I need DateTime().year().get() and DateTime().dayOfMonth().get() to always return the same date

tks


Solution

  • If you can't add a factory object as suggested by skaffman, you can use DateTimeUtils.setCurrentMillisFixed().