|
1 | 1 | package com.baeldung.jackson.xmlToJson; |
2 | 2 |
|
3 | | - |
4 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
5 | 3 | import com.fasterxml.jackson.databind.JsonNode; |
6 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
7 | 5 | import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
|
14 | 12 | public class XmlToJsonUnitTest { |
15 | 13 |
|
16 | 14 | @Test |
17 | | - public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() { |
| 15 | + public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() throws IOException{ |
18 | 16 | String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>"; |
19 | 17 |
|
20 | | - try { |
21 | | - XmlMapper xmlMapper = new XmlMapper(); |
22 | | - Flower poppy = xmlMapper.readValue(flowerXML, Flower.class); |
| 18 | + XmlMapper xmlMapper = new XmlMapper(); |
| 19 | + Flower poppy = xmlMapper.readValue(flowerXML, Flower.class); |
23 | 20 |
|
24 | | - assertEquals(poppy.getName(), "Poppy"); |
25 | | - assertEquals(poppy.getColor(), Color.RED); |
26 | | - assertEquals(poppy.getPetals(), new Integer(9)); |
| 21 | + assertEquals(poppy.getName(), "Poppy"); |
| 22 | + assertEquals(poppy.getColor(), Color.RED); |
| 23 | + assertEquals(poppy.getPetals(), new Integer(9)); |
27 | 24 |
|
28 | | - ObjectMapper mapper = new ObjectMapper(); |
29 | | - String json = mapper.writeValueAsString(poppy); |
| 25 | + ObjectMapper mapper = new ObjectMapper(); |
| 26 | + String json = mapper.writeValueAsString(poppy); |
30 | 27 |
|
31 | | - assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}"); |
32 | | - System.out.println(json); |
33 | | - } catch(IOException e) { |
34 | | - e.printStackTrace(); |
35 | | - } |
| 28 | + assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}"); |
36 | 29 | } |
37 | 30 |
|
38 | 31 | @Test |
39 | | - public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() { |
| 32 | + public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() throws IOException { |
40 | 33 | String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>"; |
41 | 34 |
|
42 | | - try { |
43 | | - XmlMapper xmlMapper = new XmlMapper(); |
44 | | - JsonNode node = xmlMapper.readTree(flowerXML.getBytes()); |
45 | | - |
46 | | - ObjectMapper jsonMapper = new ObjectMapper(); |
47 | | - String json = jsonMapper.writeValueAsString(node); |
| 35 | + XmlMapper xmlMapper = new XmlMapper(); |
| 36 | + JsonNode node = xmlMapper.readTree(flowerXML.getBytes()); |
48 | 37 |
|
49 | | - System.out.println(json); |
| 38 | + ObjectMapper jsonMapper = new ObjectMapper(); |
| 39 | + String json = jsonMapper.writeValueAsString(node); |
50 | 40 |
|
51 | | - assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}"); |
52 | | - } catch(IOException e) { |
53 | | - e.printStackTrace(); |
54 | | - } |
| 41 | + assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}"); |
55 | 42 | } |
56 | 43 | } |
0 commit comments