ResourcesUtils { public static String loadResource(String resourceName, boolean exceptionWhenNotFound) { try { URL url = Resources.getResource(resourceName); return Resources.toString(url, Charsets.UTF_8); } catch (Exception e) { log.error("Error loading resource '"+resourceName+"'", e); if(exceptionWhenNotFound){ throw new RuntimeException("Error loading required resource '"+resourceName+"'"); } return null; } } private  ResourcesUtils(); static String loadResource(String resourceName, boolean exceptionWhenNotFound);  }
@Test public void testLoadExisitingResourceNoException(TestContext context){ String content = ResourcesUtils.loadResource("ResourcesUtilsTest_TestResource", false); context.assertNotNull(content, "Resource should have been correctly loaded"); context.assertTrue(content.contains("This is a test resource for the ResourcesUtilsTest class"), "Content of resource not as expected"); }
@Test public void testLoadMissingResourceNoException(TestContext context){ String content = ResourcesUtils.loadResource("SomeUnknownResource", false); context.assertNull(content, "Resource should not have been correctly loaded"); }
@Test public void testLoadMissingResourceThrowingException(){ thrown.expect( RuntimeException.class ); thrown.expectMessage("Error loading required resource 'SomeUnknownResource'"); ResourcesUtils.loadResource("SomeUnknownResource", true); }