From 2fec9da2d5dd5955e0d1bd1e09873636132c1a70 Mon Sep 17 00:00:00 2001 From: Felipe Santiago Corro Date: Sun, 22 Jul 2018 17:36:51 -0300 Subject: [PATCH] Overriding System time for testing (#4779) * Overriding System time for testing * Remove Joda Date Time examples --- core-java-8/pom.xml | 11 +++++++++++ .../aspect/ChangeCallsToCurrentTimeInMillisMethod.aj | 9 +++++++++ .../com/baeldung/util/CurrentDateTimeUnitTest.java | 4 +++- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 core-java-8/src/main/java/com/baeldung/aspect/ChangeCallsToCurrentTimeInMillisMethod.aj diff --git a/core-java-8/pom.xml b/core-java-8/pom.xml index dee0634951..7e3b8cb280 100644 --- a/core-java-8/pom.xml +++ b/core-java-8/pom.xml @@ -99,6 +99,16 @@ joda-time ${joda.version} + + org.aspectj + aspectjrt + ${asspectj.version} + + + org.aspectj + aspectjweaver + ${asspectj.version} + @@ -170,6 +180,7 @@ 2.10 3.6.1 + 1.8.9 1.7.0 1.19 1.19 diff --git a/core-java-8/src/main/java/com/baeldung/aspect/ChangeCallsToCurrentTimeInMillisMethod.aj b/core-java-8/src/main/java/com/baeldung/aspect/ChangeCallsToCurrentTimeInMillisMethod.aj new file mode 100644 index 0000000000..b28bebfdaf --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/aspect/ChangeCallsToCurrentTimeInMillisMethod.aj @@ -0,0 +1,9 @@ +package com.baeldung.aspect; + +public aspect ChangeCallsToCurrentTimeInMillisMethod { + long around(): + call(public static native long java.lang.System.currentTimeMillis()) + && within(user.code.base.pckg.*) { + return 0; + } +} diff --git a/core-java-8/src/test/java/com/baeldung/util/CurrentDateTimeUnitTest.java b/core-java-8/src/test/java/com/baeldung/util/CurrentDateTimeUnitTest.java index 3ad3deb548..1689a5054d 100644 --- a/core-java-8/src/test/java/com/baeldung/util/CurrentDateTimeUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/util/CurrentDateTimeUnitTest.java @@ -1,6 +1,7 @@ package com.baeldung.util; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.time.Clock; import java.time.Instant; @@ -9,6 +10,8 @@ import java.time.LocalTime; import java.time.ZoneId; import java.time.temporal.ChronoField; +import org.joda.time.DateTime; +import org.joda.time.DateTimeUtils; import org.junit.Test; public class CurrentDateTimeUnitTest { @@ -39,5 +42,4 @@ public class CurrentDateTimeUnitTest { assertEquals(clock.instant().getEpochSecond(), now.getEpochSecond()); } - }