URLFetcher { public String fetchSafely(@NotNull final String url) { requireNonNull(url, "url parameter can't be null"); try { return fetch(url); } catch (IOException e) { return null; } }  String fetchSafely(@NotNull final String url); String fetch(@NotNull final String url);  }
@Test(expectedExceptions = NullPointerException.class) public void checkNullURL() { urlFetcher.fetchSafely(null); }
@Test public void checkGetContent() { URL urlJson = getClass().getClassLoader().getResource("devfile/url_fetcher_test_resource.json"); Assert.assertNotNull(urlJson); String content = urlFetcher.fetchSafely(urlJson.toString()); assertEquals(content, "Hello"); }
@Test public void checkUrlFileIsInvalid() { String result = urlFetcher.fetchSafely("hello world"); assertNull(result); }
@Test public void checkMissingContent() { URL urlJson = getClass().getClassLoader().getResource("devfile/url_fetcher_test_resource.json"); Assert.assertNotNull(urlJson); String content = urlFetcher.fetchSafely(urlJson.toString() + "-invalid"); assertNull(content); }
@Test public void checkPartialContent() { URL urlJson = getClass().getClassLoader().getResource("devfile/url_fetcher_test_resource.json"); Assert.assertNotNull(urlJson); String content = new OneByteURLFetcher().fetchSafely(urlJson.toString()); assertEquals(content, "Hello".substring(0, 1)); }