ClasspathResources { static InputStream openStream(Iterator<URL> urls, String path) throws IOException { if (urls.hasNext()) { URL url = urls.next(); if (urls.hasNext()) throw new Error("found duplicate resources with same name, path=" + path); return url.openStream(); } throw new Error("can not load resource, path=" + path); }  static String text(String path); static byte[] bytes(String path); static InputStream stream(String path);  }
@Test void openStreamWithDuplicateResources() { String path = "classpath-resource-test/resource.txt"; URL resource = Thread.currentThread().getContextClassLoader().getResource(path); assertThatThrownBy(() -> ClasspathResources.openStream(List.of(resource, resource).iterator(), path)) .isInstanceOf(Error.class) .hasMessageContaining("found duplicate resources with same name"); }