An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
January 11, 2022 06:16 am GMT
Original Link: https://dev.to/codever/how-to-read-file-from-test-resources-in-java-unit-test-43b7
How to read file from test resources in java unit test
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:
@Testvoid 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.
Original Link: https://dev.to/codever/how-to-read-file-from-test-resources-in-java-unit-test-43b7
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To