From b2eae0006dca0e121f7755b6358f12fa0f194db1 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 11 Jan 2018 10:36:58 +0100 Subject: [PATCH] Polish "Make GsonAutoConfiguration align with JacksonAutoConfiguration" Closes gh-11591 --- .../gson/GsonAutoConfigurationTests.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java index 5e2cc2cfc3c..95dc67c9b51 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java @@ -25,6 +25,7 @@ import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.LongSerializationPolicy; import org.joda.time.DateTime; import org.junit.Test; @@ -32,6 +33,7 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import static org.assertj.core.api.Assertions.assertThat; @@ -40,6 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author David Liu * @author Ivan Golovko + * @author Stephane Nicoll */ public class GsonAutoConfigurationTests { @@ -146,13 +149,23 @@ public class GsonAutoConfigurationTests { @Test public void additionalGsonBuilderCustomization() { - this.contextRunner.withUserConfiguration(GsonBuilderCustomConfig.class) + this.contextRunner.withUserConfiguration(GsonBuilderCustomizerConfig.class) .run(context -> { Gson gson = context.getBean(Gson.class); assertThat(gson.toJson(new DataObject())).isEqualTo("{}"); }); } + @Test + public void customGsonBuilder() { + this.contextRunner.withUserConfiguration(GsonBuilderConfig.class) + .run(context -> { + Gson gson = context.getBean(Gson.class); + assertThat(gson.toJson(new DataObject())) + .isEqualTo("{\"data\":1,\"owner\":null}"); + }); + } + @Test public void withPrettyPrinting() { this.contextRunner.withPropertyValues("spring.gson.pretty-printing:true") @@ -164,7 +177,7 @@ public class GsonAutoConfigurationTests { } @Test - public void withoutLenient() throws Exception { + public void withoutLenient() { this.contextRunner.run(context -> { Gson gson = context.getBean(Gson.class); /* @@ -180,7 +193,7 @@ public class GsonAutoConfigurationTests { } @Test - public void withLenient() throws Exception { + public void withLenient() { this.contextRunner.withPropertyValues("spring.gson.lenient:true").run(context -> { Gson gson = context.getBean(Gson.class); @@ -225,7 +238,8 @@ public class GsonAutoConfigurationTests { }); } - protected static class GsonBuilderCustomConfig { + @Configuration + static class GsonBuilderCustomizerConfig { @Bean public GsonBuilderCustomizer customSerializationExclusionStrategy() { @@ -245,6 +259,16 @@ public class GsonAutoConfigurationTests { } + @Configuration + static class GsonBuilderConfig { + + @Bean + public GsonBuilder customGsonBuilder() { + return new GsonBuilder().serializeNulls(); + } + + } + public class DataObject { public static final String STATIC_DATA = "bye"; @@ -252,6 +276,9 @@ public class GsonAutoConfigurationTests { @SuppressWarnings("unused") private Long data = 1L; + @SuppressWarnings("unused") + private String owner = null; + public void setData(Long data) { this.data = data; }