BAEL-87 - @JsonComponent in Spring Boot (#1519)
This commit is contained in:
committed by
Zeger Hendrikse
parent
2a76b9c656
commit
8da820b35a
@@ -0,0 +1,27 @@
|
||||
package org.baeldung.jsoncomponent;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.json.JsonTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@JsonTest
|
||||
@RunWith(SpringRunner.class)
|
||||
public class UserJsonDeserializerTest {
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Test
|
||||
public void testDeserialize() throws IOException {
|
||||
User user = objectMapper.readValue("{\"favoriteColor\":\"#f0f8ff\"}", User.class);
|
||||
assertEquals(Color.ALICEBLUE, user.getFavoriteColor());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.baeldung.jsoncomponent;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.json.JsonTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@JsonTest
|
||||
@RunWith(SpringRunner.class)
|
||||
public class UserJsonSerializerTest {
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Test
|
||||
public void testSerialization() throws JsonProcessingException {
|
||||
String json = objectMapper.writeValueAsString(new User(Color.ALICEBLUE));
|
||||
assertEquals("{\"favoriteColor\":\"#f0f8ff\"}", json);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user