Introduce Resource::getContentAsString
This commit introduces the getContentAsString method to Resource, returning the string contents of the resource. Closes gh-24651
This commit is contained in:
committed by
Arjen Poutsma
parent
18896aceca
commit
4da2499613
@@ -456,4 +456,25 @@ class ResourceTests {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void getContentAsString_givenValidFile_ShouldReturnFileContent() throws IOException {
|
||||
String expectedString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
||||
+ "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n"
|
||||
+ "<properties version=\"1.0\">\n"
|
||||
+ "\t<entry key=\"foo\">bar</entry>\n"
|
||||
+ "</properties>";
|
||||
|
||||
String fileDirString =
|
||||
new ClassPathResource("org/springframework/core/io/example.xml").getContentAsString();
|
||||
assertThat(fileDirString).isNotBlank();
|
||||
assertThat(fileDirString).isEqualTo(expectedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getContentAsString_givenAnInvalidFile_ShouldThrowFileNotFoundException(){
|
||||
assertThatExceptionOfType(FileNotFoundException.class)
|
||||
.isThrownBy(new ClassPathResource("nonExistantFile")::getContentAsString);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user