Use the getClass().getClassLoader().getResourceAsStream()
to get an InputStream
for the wanted resource. In the following snippet we access the bookmark-example.xml
file resource, which is placed directly in the src/test/resources
folder:
@Test void shouldUnmarshallXmlToJava() throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance("dev.codepal.bookmark"); final var jaxbUnmarshaller = jaxbContext.createUnmarshaller(); InputStream inStream = getClass().getClassLoader().getResourceAsStream( "bookmark-example.xml"); JAXBElement<Bookmark> o = (JAXBElement<Bookmark>)jaxbUnmarshaller.unmarshal(inStream); Bookmark bookmark = o.getValue(); assertThat(bookmark.getTitle(), equalTo("CodepediaOrg")); }
Shared with ❤️ from Codever. Use 👉 copy to mine functionality to add it to your personal snippets collection.
Top comments (0)