This commit is contained in:
Phillip Webb
2017-04-17 22:08:51 -07:00
parent 3fe0b844a6
commit f46d799f31
8 changed files with 37 additions and 24 deletions

View File

@@ -113,6 +113,10 @@ public abstract class AbstractJsonMarshalTester<T> {
return this.type;
}
/**
* Return class used to load relative resources.
* @return the resource load class
*/
protected final Class<?> getResourceLoadClass() {
return this.resourceLoadClass;
}

View File

@@ -152,13 +152,14 @@ public class JacksonTester<T> extends AbstractJsonMarshalTester<T> {
}
/**
* Returns a new instance of {@link JacksonTester} with the view
* that should be used for json serialization/deserialization.
* Returns a new instance of {@link JacksonTester} with the view that should be used
* for json serialization/deserialization.
* @param view the view class
* @return the new instance
*/
public JacksonTester<T> forView(Class<?> view) {
return new JacksonTester<T>(this.getResourceLoadClass(), this.getType(), this.objectMapper, view);
return new JacksonTester<T>(this.getResourceLoadClass(), this.getType(),
this.objectMapper, view);
}
/**

View File

@@ -64,4 +64,3 @@ public class ExampleObject {
}
}

View File

@@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Integration tests for {@link JacksonTester}. Shows typical usage.
*
* @author Phillip Webb
* @author Madhura Bhave
*/
public class JacksonTesterIntegrationTests {
@@ -56,6 +57,13 @@ public class JacksonTesterIntegrationTests {
JacksonTester.initFields(this, this.objectMapper);
}
@Test
public void typicalTest() throws Exception {
String example = JSON;
assertThat(this.simpleJson.parse(example).getObject().getName())
.isEqualTo("Spring");
}
@Test
public void typicalListTest() throws Exception {
String example = "[" + JSON + "]";
@@ -79,10 +87,9 @@ public class JacksonTesterIntegrationTests {
ExampleObjectWithView object = new ExampleObjectWithView();
object.setName("Spring");
object.setAge(123);
JsonContent<ExampleObjectWithView> content = this.jsonWithView.forView(
ExampleObjectWithView.TestView.class).write(object);
assertThat(content).extractingJsonPathStringValue("@.name")
.isEqualTo("Spring");
JsonContent<ExampleObjectWithView> content = this.jsonWithView
.forView(ExampleObjectWithView.TestView.class).write(object);
assertThat(content).extractingJsonPathStringValue("@.name").isEqualTo("Spring");
assertThat(content).doesNotHaveJsonPathValue("age");
}
@@ -90,10 +97,9 @@ public class JacksonTesterIntegrationTests {
public void readWithResourceAndView() throws Exception {
this.objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
ByteArrayResource resource = new ByteArrayResource(JSON.getBytes());
ObjectContent<ExampleObjectWithView> content = this.jsonWithView.forView(
ExampleObjectWithView.TestView.class).read(resource);
assertThat(content.getObject().getName())
.isEqualTo("Spring");
ObjectContent<ExampleObjectWithView> content = this.jsonWithView
.forView(ExampleObjectWithView.TestView.class).read(resource);
assertThat(content.getObject().getName()).isEqualTo("Spring");
assertThat(content.getObject().getAge()).isEqualTo(0);
}
@@ -101,10 +107,9 @@ public class JacksonTesterIntegrationTests {
public void readWithReaderAndView() throws Exception {
this.objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
Reader reader = new StringReader(JSON);
ObjectContent<ExampleObjectWithView> content = this.jsonWithView.forView(
ExampleObjectWithView.TestView.class).read(reader);
assertThat(content.getObject().getName())
.isEqualTo("Spring");
ObjectContent<ExampleObjectWithView> content = this.jsonWithView
.forView(ExampleObjectWithView.TestView.class).read(reader);
assertThat(content.getObject().getName()).isEqualTo("Spring");
assertThat(content.getObject().getAge()).isEqualTo(0);
}