{"id":25776889,"url":"https://github.com/faketime-java/faketime","last_synced_at":"2025-02-27T06:07:00.972Z","repository":{"id":54433568,"uuid":"102333436","full_name":"faketime-java/faketime","owner":"faketime-java","description":"Fake currentTimeMillis() without class loader hacks","archived":false,"fork":false,"pushed_at":"2021-02-18T10:45:52.000Z","size":264,"stargazers_count":106,"open_issues_count":0,"forks_count":29,"subscribers_count":10,"default_branch":"master","last_synced_at":"2023-12-07T22:03:11.220Z","etag":null,"topics":["java","junit","maven","testing","time"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/faketime-java.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-04T07:45:37.000Z","updated_at":"2023-11-24T11:26:01.000Z","dependencies_parsed_at":"2022-08-13T15:31:10.047Z","dependency_job_id":null,"html_url":"https://github.com/faketime-java/faketime","commit_stats":null,"previous_names":[],"tags_count":8,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faketime-java%2Ffaketime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faketime-java%2Ffaketime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faketime-java%2Ffaketime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faketime-java%2Ffaketime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/faketime-java","download_url":"https://codeload.github.com/faketime-java/faketime/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240987435,"owners_count":19889335,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","junit","maven","testing","time"],"created_at":"2025-02-27T06:01:32.805Z","updated_at":"2025-02-27T06:07:00.964Z","avatar_url":"https://github.com/faketime-java.png","language":"Java","readme":"# FakeTime for Java\n\u003e FakeTime uses a native Java agent to replace `System.currentTimeMillis()` implementation with the one you can control using system properties.\n\n\u003e Inspired by [arvindsv/faketime](https://github.com/arvindsv/faketime).\n\n[![Build Status](https://travis-ci.org/faketime-java/faketime.svg?branch=master)](https://travis-ci.org/faketime-java/faketime)\n\n```java\npublic class ExamRegistrationServiceTest implements FakeTimeMixin {\n  \n  @Autowired\n  ExamRegistrationService examRegistrationService;\n  \n  @Test\n  public void registrationExpiresAfterGivenPeriod() {\n    ExamRegistration registration = examRegistrationService.openRegistrationValidFor(Duration.ofDays(5));\n    \n    offsetRealTimeByDays(5);\n    \n    assertThat(registration.hasExpired()).isTrue();\n  }\n  \n  @Test\n  public void registrationIsValidDuringGivenPeriod() {\n    ExamRegistration registration = examRegistrationService.openRegistrationValidFor(Duration.ofDays(5));\n      \n    offsetRealTimeBy(Duration.ofDays(5).minusMinutes(1));\n      \n    assertThat(registration.hasExpired()).isFalse();\n  }\n  \n  @After\n  public void restoreRealTimeAfterTest() {\n    restoreRealTime();\n  }\n}\n```\n\n## Manual setup\nStart faking time in 4 easy steps:\n1. Download the `faketime-agent.jar` for your operating system from the [Maven Central](https://mvnrepository.com/artifact/io.github.faketime-java/faketime-agent) repository.\n```xml\n\u003c!-- Windows 32bit --\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n  \u003cartifactId\u003efaketime-agent\u003c/artifactId\u003e\n  \u003cversion\u003e0.8.0\u003c/version\u003e\n  \u003cclassifier\u003ewindows32\u003c/classifier\u003e\n\u003c/dependency\u003e\n\n\u003c!-- Windows 64bit --\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n  \u003cartifactId\u003efaketime-agent\u003c/artifactId\u003e\n  \u003cversion\u003e0.8.0\u003c/version\u003e\n  \u003cclassifier\u003ewindows64\u003c/classifier\u003e\n\u003c/dependency\u003e\n\n\u003c!-- macOS 32bit --\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n  \u003cartifactId\u003efaketime-agent\u003c/artifactId\u003e\n  \u003cversion\u003e0.8.0\u003c/version\u003e\n  \u003cclassifier\u003emac32\u003c/classifier\u003e\n\u003c/dependency\u003e\n\n\u003c!-- macOS 64bit --\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n  \u003cartifactId\u003efaketime-agent\u003c/artifactId\u003e\n  \u003cversion\u003e0.8.0\u003c/version\u003e\n  \u003cclassifier\u003emac64\u003c/classifier\u003e\n\u003c/dependency\u003e\n\n\u003c!-- Linux 32bit --\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n  \u003cartifactId\u003efaketime-agent\u003c/artifactId\u003e\n  \u003cversion\u003e0.8.0\u003c/version\u003e\n  \u003cclassifier\u003elinux32\u003c/classifier\u003e\n\u003c/dependency\u003e\n\n\u003c!-- Linux 64bit --\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n  \u003cartifactId\u003efaketime-agent\u003c/artifactId\u003e\n  \u003cversion\u003e0.8.0\u003c/version\u003e\n  \u003cclassifier\u003elinux64\u003c/classifier\u003e\n\u003c/dependency\u003e\n```\n2. Unpack the `jar` to get the agent, which is `faketime.dll` on Windows and `libfaketime` on other systems.\n3. Attach the agent to your Java program with following JVM arguments.\n```bash\n-agentpath:path/to/unpacked/faketime/binary\n-XX:+UnlockDiagnosticVMOptions\n-XX:DisableIntrinsic=_currentTimeMillis\n-XX:CompileCommand=quiet\n-XX:CompileCommand=exclude,java/lang/System.currentTimeMillis\n-XX:CompileCommand=exclude,jdk/internal/misc/VM.getNanoTimeAdjustment\n```\n4. Use system properties to manipulate `System.currentTimeMillis()`.\n```java\nSystem.out.println(System.currentTimeMillis()); // 1234567890\nSystem.setProperty(\"faketime.offset.ms\", \"-7890\");\nSystem.out.println(System.currentTimeMillis()); // 1234560000\n\nSystem.setProperty(\"faketime.absolute.ms\", \"12345\");\nSystem.out.println(System.currentTimeMillis()); // 12345\n```\n## Java 8 API\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n  \u003cartifactId\u003efaketime-api\u003c/artifactId\u003e\n  \u003cversion\u003e0.8.0\u003c/version\u003e\n  \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\nIn case you get tired of converting everything to milliseconds there is a Java Time based API.\n```java\nFakeTime.stopAt(LocalDateTime.of(2000, 11, 10, 9, 8, 7));\nFakeTime.stopAt(2000, 11, 10, ZoneOffset.UTC);\nFakeTime.offsetRealByMinutes(100);\nFakeTime.offsetRealBy(Duration.ofHours(20));\nFakeTime.restoreReal();\n```\nAnd in case you get annoyed by writing `FakeTime` all the time there is a handy mixin.\n```java\npublic class MyTest implements FakeTimeMixin {\n  \n  @Test\n  public void someTimeTest() {\n    stopTimeAt(LocalDateTime.of(2000, 11, 10, 9, 8, 7));\n    stopTimeAt(2000, 11, 10, ZoneOffset.UTC);\n    offsetRealTimeByMinutes(100);\n    offsetRealTimeBy(Duration.ofHours(20));\n  }\n  \n  @After\n  public void restoreRealTimeAfterTest() {\n    restoreRealTime();\n  }\n}\n```\n## JUnit rule\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n  \u003cartifactId\u003efaketime-junit\u003c/artifactId\u003e\n  \u003cversion\u003e0.8.0\u003c/version\u003e\n  \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\nThis rule calls `FakeTime.restoreReal()` after every test, so you don't have to.\n\n_Note: when using `faketime-junit` you don't need to add `faketime-api` as a `test` dependency_\n```java\npublic class MyTest implements FakeTimeMixin {\n  \n  @Rule\n  public FakeTimeRule fakeTimeRule = new FakeTimeRule();\n  \n  @Test\n  public void someTimeTest() {\n    stopTimeAt(2000, 11, 10);\n    \n    assertThat(LocalDate.now()).isEqualTo(LocalDate.of(2000, 11, 10));\n    \n    // next test will start with real time\n  }\n}\n```\n## Maven plugin\nFor further convenience there is a Maven plugin that downloads and unpacks the correct agent for your operating system.\nIt then sets a property that you can use in `surefire` or `failsafe` plugins to attach the agent.\n\n\u003e Full example [here](https://github.com/faketime-java/faketime/blob/master/e2e-tests/pom.xml)\n```xml\n\u003cbuild\u003e\n  \u003cplugins\u003e\n    \u003cplugin\u003e\n      \u003cgroupId\u003eorg.apache.maven.plugins\u003c/groupId\u003e\n      \u003cartifactId\u003emaven-surefire-plugin\u003c/artifactId\u003e\n      \u003cversion\u003e2.22.1\u003c/version\u003e\n      \u003cconfiguration\u003e\n        \u003cargLine\u003e${faketime.argLine}\u003c/argLine\u003e\n      \u003c/configuration\u003e\n    \u003c/plugin\u003e\n    \n    \u003cplugin\u003e\n      \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n      \u003cartifactId\u003efaketime-maven-plugin\u003c/artifactId\u003e\n      \u003cversion\u003e0.8.0\u003c/version\u003e\n      \u003cexecutions\u003e\n        \u003cexecution\u003e\n          \u003cgoals\u003e\n            \u003cgoal\u003eprepare\u003c/goal\u003e\n          \u003c/goals\u003e\n        \u003c/execution\u003e\n      \u003c/executions\u003e\n    \u003c/plugin\u003e\n  \u003c/plugins\u003e\n\u003c/build\u003e\n```\n## Maven + IntelliJ\nIntelliJ has a cool feature that reads `argLine` from `pom.xml` and adds all arguments to the IDE test runner.\nThe only thing you need to do is to replace `${faketime.argLine}` with literal arguments, since IntelliJ is not aware of `${faketime.argLine}`.\n\n_Note: before running tests from IntelliJ make sure `faketime-maven-plugin` has downloaded the agent, otherwise tests won't start_\n\n\u003e Full example [here](https://github.com/faketime-java/faketime/blob/master/e2e-tests/pom.xml)\n```xml\n\u003cproperties\u003e\n  \u003cfaketime.binary\u003elibfaketime\u003c/faketime.binary\u003e\n\u003c/properties\u003e\n\n\u003cprofiles\u003e\n  \u003cprofile\u003e\n    \u003cid\u003efaketimeBinary\u003c/id\u003e\n    \u003cactivation\u003e\n      \u003cos\u003e\n        \u003cfamily\u003ewindows\u003c/family\u003e\n      \u003c/os\u003e\n    \u003c/activation\u003e\n    \u003cproperties\u003e\n      \u003cfaketime.binary\u003efaketime.dll\u003c/faketime.binary\u003e\n    \u003c/properties\u003e\n  \u003c/profile\u003e\n\u003c/profiles\u003e\n\n\u003cbuild\u003e\n  \u003cplugins\u003e\n    \u003cplugin\u003e\n      \u003cgroupId\u003eorg.apache.maven.plugins\u003c/groupId\u003e\n      \u003cartifactId\u003emaven-failsafe-plugin\u003c/artifactId\u003e\n      \u003cversion\u003e2.22.1\u003c/version\u003e\n      \u003cconfiguration\u003e\n        \u003cargLine\u003e\n          -agentpath:${project.build.directory}/${faketime.binary}\n          -XX:+UnlockDiagnosticVMOptions\n          -XX:DisableIntrinsic=_currentTimeMillis\n          -XX:CompileCommand=quiet\n          -XX:CompileCommand=exclude,java/lang/System.currentTimeMillis\n          -XX:CompileCommand=exclude,jdk/internal/misc/VM.getNanoTimeAdjustment\n        \u003c/argLine\u003e\n      \u003c/configuration\u003e\n    \u003c/plugin\u003e\n    \n    \u003cplugin\u003e\n      \u003cgroupId\u003eio.github.faketime-java\u003c/groupId\u003e\n      \u003cartifactId\u003efaketime-maven-plugin\u003c/artifactId\u003e\n      \u003cversion\u003e0.8.0\u003c/version\u003e\n      \u003cexecutions\u003e\n        \u003cexecution\u003e\n          \u003cgoals\u003e\n            \u003cgoal\u003eprepare\u003c/goal\u003e\n          \u003c/goals\u003e\n        \u003c/execution\u003e\n      \u003c/executions\u003e\n    \u003c/plugin\u003e\n  \u003c/plugins\u003e\n\u003c/build\u003e\n```\n## Maven + Eclipse\n_Note: before running tests from Eclipse make sure `faketime-maven-plugin` has downloaded the agent, otherwise tests won't start_\n\n`Preferences \u003e Java \u003e Installed JREs \u003e Select \u003e Edit \u003e Default VM arguments`\n```bash\n# if you're on Windows\n-agentpath:target/faketime.dll\n-XX:+UnlockDiagnosticVMOptions\n-XX:DisableIntrinsic=_currentTimeMillis\n-XX:CompileCommand=quiet\n-XX:CompileCommand=exclude,java/lang/System.currentTimeMillis\n-XX:CompileCommand=exclude,jdk/internal/misc/VM.getNanoTimeAdjustment\n\n# if you're on macOS/Linux\n-agentpath:target/libfaketime\n-XX:+UnlockDiagnosticVMOptions\n-XX:DisableIntrinsic=_currentTimeMillis\n-XX:CompileCommand=quiet\n-XX:CompileCommand=exclude,java/lang/System.currentTimeMillis\n-XX:CompileCommand=exclude,jdk/internal/misc/VM.getNanoTimeAdjustment\n```\n## Gradle\nThere are no instructions for Gradle yet, but if you'll figure this out, then don't be shy to make a pull request.\nBasically, you need to download and unpack the correct agent artifact and then add some JVM arguments to the test runner.\n","funding_links":[],"categories":["测试"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaketime-java%2Ffaketime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffaketime-java%2Ffaketime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaketime-java%2Ffaketime/lists"}