ResourceLocator { public static URL getResource(final String resource) throws MalformedURLException { final List<ClassLoader> classLoaders = Arrays.asList(Thread.currentThread().getContextClassLoader(), ResourceLocator.class.getClassLoader(), ClassLoader.getSystemClassLoader()); final URL url = getResource(classLoaders, resource); return url == null ? new File(resource).toURI().toURL() : url; } private  ResourceLocator(); static URL getResource(final String resource);  }
@Test public void testLocateProjectLocalResource() throws MalformedURLException, URISyntaxException { final URL resource = ResourceLocator.getResource("empty.file"); assertThat(resource, notNullValue()); assertThat(new File(resource.toURI()).exists(), equalTo(Boolean.TRUE)); }
@Test public void testLocateResourceOtsideOfTheProject() throws IOException { final File tmpFile = tmpFolder.newFile(); final URL resource = ResourceLocator.getResource(tmpFile.getPath()); assertThat(resource, notNullValue()); assertThat(resource, equalTo(tmpFile.toURI().toURL())); }
@Test public void testLocateNotExistentResource() throws MalformedURLException, URISyntaxException { final URL resource = ResourceLocator.getResource("some.file"); assertThat(resource, notNullValue()); assertThat(new File(resource.toURI()).exists(), equalTo(Boolean.FALSE)); }