{"id":"29d7c44f8bf65efe","repo":"junit-team/junit5","slug":"could-not-find-any-resource-s-with-name-this-c","errorCode":null,"errorMessage":"Could not find any resource(s) with name: ${this.classpathResourceName}","messagePattern":"Could not find any resource\\(s\\) with name: (.+?)","errorType":"exception","errorClass":"PreconditionViolationException","httpStatus":null,"severity":"error","filePath":"junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/ClasspathResourceSelector.java","lineNumber":132,"sourceCode":"\t/**\n\t * Get the selected {@link Resource resources}.\n\t *\n\t * <p>If the {@link Resource resources} were not provided, but only their name,\n\t * this method attempts to lazily load the {@link Resource resources} based on\n\t * their name and throws a {@link PreconditionViolationException} if the\n\t * resource cannot be loaded.\n\t *\n\t * @since 1.14\n\t */\n\t@API(status = MAINTAINED, since = \"1.14\")\n\tpublic Set<Resource> getResources() {\n\t\tif (this.resources == null) {\n\t\t\tTry<Set<Resource>> tryToGetResource = ResourceSupport.tryToGetResources(this.classpathResourceName);\n\t\t\tSet<Resource> classpathResources = tryToGetResource.getNonNullOrThrow( //\n\t\t\t\tcause -> new PreconditionViolationException( //\n\t\t\t\t\t\"Could not load resource(s) with name: \" + this.classpathResourceName, cause));\n\t\t\tif (classpathResources.isEmpty()) {\n\t\t\t\tthrow new PreconditionViolationException(\n\t\t\t\t\t\"Could not find any resource(s) with name: \" + this.classpathResourceName);\n\t\t\t}\n\t\t\tthis.resources = unmodifiableSet(classpathResources);\n\t\t}\n\t\treturn this.resources;\n\t}\n\n\t/**\n\t * Get the selected {@code FilePosition} within the classpath resource.\n\t */\n\tpublic Optional<FilePosition> getPosition() {\n\t\treturn Optional.ofNullable(this.position);\n\t}\n\n\t/**\n\t * @since 1.3\n\t */\n\t@API(status = STABLE, since = \"1.3\")","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/ClasspathResourceSelector.java#L114-L150","documentation":"Thrown by ClasspathResourceSelector.getResources() (line 125-138) when lazy resolution via ResourceSupport.tryToGetResources succeeds but returns an empty set, i.e. no classpath/module-path entry contains a resource with the given name. It is a PreconditionViolationException, distinct from the 'Could not load' variant which fires when the lookup itself fails (line 128-130).","triggerScenarios":"Calling DiscoverySelectors.selectClasspathResource(name) and then getResources()/getClasspathResources() (or letting an engine resolve it) when 'name' does not match any resource resolvable by the context ClassLoader. The constructor strips a leading '/' so a name like '/foo.json' becomes 'foo.json'; mismatches in package-style paths matter.","commonSituations":"Resource lives in src/test/resources but the test runs with a classpath that omits it (shading/relocating); wrong path separator or case sensitivity on case-sensitive filesystems; resource packaged in a jar that isn't on the classpath; module-path resource not opened/exports correctly; typo in the resource name.","solutions":["Confirm the resource exists: getClass().getClassLoader().getResource(name) returns non-null before building the selector.","Drop any leading '/' (the constructor already does, so passing 'com/foo/data.json' directly is correct).","If the resource is in a named module, ensure the containing package is open to the framework (opens/exports) so the resource is resolvable.","Rebuild the project to ensure src/test/resources is copied to the output directory / packaged into the jar under test."],"exampleFix":"// before\nvar selector = DiscoverySelectors.selectClasspathResource(\"com/example/data.json\");\n// ... resources missing on classpath -> throws on resolution\n\n// after\nString name = \"com/example/data.json\";\nif (Thread.currentThread().getContextClassLoader().getResource(name) == null) {\n    throw new IllegalStateException(\"resource not on classpath: \" + name);\n}\nvar selector = DiscoverySelectors.selectClasspathResource(name);","handlingStrategy":"validation","validationCode":"String name = \"com/example/data.json\";\nClassLoader cl = Thread.currentThread().getContextClassLoader();\nboolean exists = cl.getResource(name) != null;\nif (!exists) throw new IllegalStateException(\"resource not on classpath: \" + name);\nvar selector = DiscoverySelectors.selectClasspathResource(name);","typeGuard":null,"tryCatchPattern":"try {\n    selector.getResources(); // forces lazy resolution\n} catch (PreconditionViolationException e) {\n    throw new IllegalStateException(\"classloaders missing the resource\", e);\n}","preventionTips":["Confirm getClass().getClassLoader().getResource(name) is non-null before building the selector.","For named modules, ensure the owning package is open to the framework.","Do not prefix the name with a leading slash (the constructor strips one anyway)."],"tags":["junit-platform","discovery","classpath","resources","classpath-resource-selector"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}