@Test public void generates_dates_at_current_date_within_plus_or_minus_two_years() throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); Date nowDate = formatter.parse("2000/01/01 00:00:00"); Date plusTwoYears = formatter.parse("2002/01/01 00:00:00"); Date minusTwoYears = formatter.parse("1998/01/01 00:00:00"); when(mockTimeProvider.getCurrentTimeInMilliseconds()).thenReturn(nowDate.getTime()); int runCount = 1000; for (int i = 0; i < runCount; i++) { Object result = this.dateGenerator.create(Date.class, null); Date date = (Date)result; assertThat(date, greaterThanOrEqualTo(minusTwoYears)); assertThat(date, lessThanOrEqualTo(plusTwoYears)); } }
@Override public Object create(Object request, SpecimenContext context) { if (!request.equals(Date.class)) { return new NoSpecimen(); } Date nowDate = new Date(this.timeProvider.getCurrentTimeInMilliseconds()); long min = getTwoYearsAgoMs(nowDate); long max = getTwoYearsAheadMs(nowDate); long date = FixtureMath.randLong(min, max); return new Date(date); }
DateGenerator implements SpecimenBuilder { @Override public Object create(Object request, SpecimenContext context) { if (!request.equals(Date.class)) { return new NoSpecimen(); } Date nowDate = new Date(this.timeProvider.getCurrentTimeInMilliseconds()); long min = getTwoYearsAgoMs(nowDate); long max = getTwoYearsAheadMs(nowDate); long date = FixtureMath.randLong(min, max); return new Date(date); } }
DateGenerator implements SpecimenBuilder { @Override public Object create(Object request, SpecimenContext context) { if (!request.equals(Date.class)) { return new NoSpecimen(); } Date nowDate = new Date(this.timeProvider.getCurrentTimeInMilliseconds()); long min = getTwoYearsAgoMs(nowDate); long max = getTwoYearsAheadMs(nowDate); long date = FixtureMath.randLong(min, max); return new Date(date); } DateGenerator();  DateGenerator(TimeProvider timeProvider); }
DateGenerator implements SpecimenBuilder { @Override public Object create(Object request, SpecimenContext context) { if (!request.equals(Date.class)) { return new NoSpecimen(); } Date nowDate = new Date(this.timeProvider.getCurrentTimeInMilliseconds()); long min = getTwoYearsAgoMs(nowDate); long max = getTwoYearsAheadMs(nowDate); long date = FixtureMath.randLong(min, max); return new Date(date); } DateGenerator();  DateGenerator(TimeProvider timeProvider); @Override Object create(Object request, SpecimenContext context); }
DateGenerator implements SpecimenBuilder { @Override public Object create(Object request, SpecimenContext context) { if (!request.equals(Date.class)) { return new NoSpecimen(); } Date nowDate = new Date(this.timeProvider.getCurrentTimeInMilliseconds()); long min = getTwoYearsAgoMs(nowDate); long max = getTwoYearsAheadMs(nowDate); long date = FixtureMath.randLong(min, max); return new Date(date); } DateGenerator();  DateGenerator(TimeProvider timeProvider); @Override Object create(Object request, SpecimenContext context);  }