{"id":16514618,"url":"https://github.com/danielliu1123/classpath-replacer","last_synced_at":"2025-06-29T07:37:58.420Z","repository":{"id":83980551,"uuid":"600314493","full_name":"DanielLiu1123/classpath-replacer","owner":"DanielLiu1123","description":"Change the classpath in unit tests.","archived":false,"fork":false,"pushed_at":"2025-06-08T18:00:50.000Z","size":454,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-08T18:39:28.163Z","etag":null,"topics":["classpath","java","junit5","testing-framework"],"latest_commit_sha":null,"homepage":"https://danielliu1123.github.io/classpath-replacer/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DanielLiu1123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-02-11T05:12:21.000Z","updated_at":"2025-06-08T18:00:55.000Z","dependencies_parsed_at":"2023-02-27T08:00:48.040Z","dependency_job_id":"48d4b14a-7e50-4df7-9b71-60ca3e8003c7","html_url":"https://github.com/DanielLiu1123/classpath-replacer","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/DanielLiu1123/classpath-replacer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielLiu1123%2Fclasspath-replacer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielLiu1123%2Fclasspath-replacer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielLiu1123%2Fclasspath-replacer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielLiu1123%2Fclasspath-replacer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DanielLiu1123","download_url":"https://codeload.github.com/DanielLiu1123/classpath-replacer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielLiu1123%2Fclasspath-replacer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262558552,"owners_count":23328557,"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":["classpath","java","junit5","testing-framework"],"created_at":"2024-10-11T16:13:07.237Z","updated_at":"2025-06-29T07:37:58.405Z","avatar_url":"https://github.com/DanielLiu1123.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Classpath Replacer\n\n[![Build](https://img.shields.io/github/actions/workflow/status/DanielLiu1123/classpath-replacer/build.yml?branch=main)](https://github.com/DanielLiu1123/classpath-replacer/actions)\n[![Maven Central](https://img.shields.io/maven-central/v/io.github.danielliu1123/classpath-replacer)](https://search.maven.org/artifact/io.github.danielliu1123/classpath-replacer)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n`Classpath Replacer` is essential for writing unit tests that require different classpath. \n\nIf you need to test different classpath scenarios, this library is must-had.\n\n## When you need it\n\nIf you are a library developer, then you may need to test the behavior of your library under different classpath.\n\nFor example, you just write a [`JsonUtil`](examples/junit5/src/main/java/com/example/JsonUtil.java), you are only\nresponsible for providing the interface, and the specific implementation is up to the user to choose, such as\nusing `Jackson` or `Gson`.\n\nThen you need to test your library in different scenarios:\n\n- With `Jackson`, without `Gson`\n- With `Gson`, without `Jackson`\n- With both `Jackson` and `Gson`\n- Different versions of behaviors (e.g. `Gson` 2.8.9 and 2.9.0)\n\nBut it is challenging to simulate this scenario in unit tests, because usually, unit tests are run under a fixed\nclasspath.\n\n`Classpath Replacer` can help you simulate this scenario in your unit tests.\n\n## How to use\n\n```groovy\n// Gradle\ntestImplementation \"io.github.danielliu1123:classpath-replacer:\u003clatest\u003e\"\n```\n\n```xml\n\u003c!-- Maven --\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.danielliu1123\u003c/groupId\u003e\n    \u003cartifactId\u003eclasspath-replacer\u003c/artifactId\u003e\n    \u003cversion\u003elatest\u003c/version\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\n\n`@Classpath` is the core annotation of this framework. It can be used on the test class or test method, method-level annotation will override the class-level annotation.\n\nExamples:\n\n```java\n// add spring-boot:3.0.0 and its transitive dependencies to the classpath.\n@Classpath(add = \"org.springframework.boot:spring-boot:3.0.0\")\n\n// Exclude spring-boot-3.0.0.jar from the classpath, but not include its transitive dependencies.\n// value uses the jar package name.\n@Classpath(exclude = \"spring-boot-3.0.0.jar\")\n\n// Exclude spring-boot-3.0.0.jar from the classpath, but not include its transitive dependencies.\n// value uses the maven coordinate.\n@Classpath(exclude = \"org.springframework.boot:spring-boot:3.0.0\")\n\n// Exclude all versions of spring-boot jars in the classpath. \n// Using jar package name can't exclude transitive dependencies.\n@Classpath(exclude = \"spring-boot-*.jar\")\n\n// If you want to exclude all versions of spring-boot jars, just omit the version\n@Classpath(exclude = \"org.springframework.boot:spring-boot\")\n\n// Using maven coordinate doesn't exclude the transitive dependencies by default, you can set `excludeTransitive` to true.\n@Classpath(exclude = \"org.springframework.boot:spring-boot:3.0.0\", excludeTransitive = true)\n\n// exclude all versions of spring-boot jars and their transitive dependencies\n@Classpath(exclude = \"org.springframework.boot:spring-boot\", excludeTransitive = true)\n```\n\nFor the test scenarios of the above `JsonUtil`, you can write the following tests:\n\n```java\nclass JsonUtilTest {\n\n  @Test\n  void testNoJsonImplementationOnClasspath() {\n    assertThrows(ExceptionInInitializerError.class, JsonUtil::instance);\n  }\n\n  @Test\n  @Classpath(add = \"com.google.code.gson:gson:2.10.1\")\n  void testGsonOnClasspath() {\n    assertTrue(JsonUtil.instance() instanceof Gson);\n    assertEquals(\"{}\", JsonUtil.toJson(new Object()));\n  }\n\n  @Test\n  @Classpath(add = \"com.fasterxml.jackson.core:jackson-databind:2.14.1\")\n  void testJacksonOnClasspath() {\n    assertTrue(JsonUtil.instance() instanceof Jackson);\n  }\n\n  @Test\n  @Classpath(add = {\"com.fasterxml.jackson.core:jackson-databind:2.14.1\", \"com.google.code.gson:gson:2.10.1\"})\n  void useJacksonFirst_whenBothJacksonAndGsonOnClasspath() {\n    assertTrue(JsonUtil.instance() instanceof Jackson);\n  }\n}\n```\n\n## Limitation\n\n**The static field will not be reusable!**\n\n```java\npublic class StaticMethodTests {\n\n  static AtomicInteger counter = new AtomicInteger(0);\n\n  @Test\n  @Classpath\n  void test1() {\n    assertEquals(0, counter.getAndIncrement()); // pass\n  }\n\n  @Test\n  @Classpath\n  void test2() {\n    assertEquals(0, counter.getAndIncrement()); // pass\n  }\n\n}\n```\n\nBecause each test method has a different classpath, it causes the test class to be reloaded, and static field/blocks will also be reinitialized.\n\nIf you want to use `@Classpath` with `@SpringBootTest`, you need to consider the side effects that may come with restarting the Spring context.\n\nIf you want to use `@Classpath` with [Testcontainers](https://www.testcontainers.org/), you need to consider the side effects that may come with restarting the container.\n\n## Thanks\n\nThis project is inspired\nby [spring-boot-test-support](https://github.com/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot-tools/spring-boot-test-support).\nHowever, this library is only for internal use in `Spring Boot` and does not provide Maven coordinates for external use,\nso `Classpath Replacer` was born.\n\n## License\n\nThe MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielliu1123%2Fclasspath-replacer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielliu1123%2Fclasspath-replacer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielliu1123%2Fclasspath-replacer/lists"}