JaxbDemoTest.java
Download JaxbDemoTest.java
1: package com.wpollock.jaxbdemo;
2:
3: import org.junit.jupiter.api.AfterAll;
4: import org.junit.jupiter.api.BeforeAll;
5: import org.junit.jupiter.api.DisplayName;
6: import org.junit.jupiter.api.Test;
7:
8: import static org.junit.jupiter.api.Assertions.*;
9: //import org.junit.After;
10: //import org.junit.Before;
11: import javax.xml.bind.*;
12: import java.io.*;
13: import java.math.BigDecimal;
14:
15: class ProductToXmlTest {
16: private static Product product;
17:
18: @BeforeAll
19: static void setUp() {
20: long l = 10;
21: Long longId = l;
22: User user = new User(longId, "Hymie", "HPiffl@wpollock.com");
23: product = new Product("PO1", "Spring Guru Mug", "https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg", new BigDecimal(18.95), user);
24: }
25:
26: @AfterAll
27: static void tearDown() {
28: product = null;
29: }
30:
31: @Test
32: @DisplayName("Object to XML")
33: public void testObjectToXml() throws JAXBException, FileNotFoundException {
34: JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
35: Marshaller marshaller = jaxbContext.createMarshaller();
36: marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
37: marshaller.marshal(product, new File("product.xml"));
38: marshaller.marshal(product, System.out);
39: }
40: }