pull/9539/head
Stephane Nicoll 8 years ago
parent c5c6a526d3
commit 31ee45bf36

@ -18,6 +18,7 @@ package org.springframework.boot.jackson;
import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -31,32 +32,35 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class JsonComponentModuleTests { public class JsonComponentModuleTests {
private AnnotationConfigApplicationContext context;
@After
public void closeContext() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void moduleShouldRegisterSerializers() throws Exception { public void moduleShouldRegisterSerializers() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( load(OnlySerializer.class);
JsonComponentModule.class, OnlySerializer.class); JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
JsonComponentModule module = context.getBean(JsonComponentModule.class);
assertSerialize(module); assertSerialize(module);
context.close();
} }
@Test @Test
public void moduleShouldRegisterDeserializers() throws Exception { public void moduleShouldRegisterDeserializers() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( load(OnlyDeserializer.class);
JsonComponentModule.class, OnlyDeserializer.class); JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
JsonComponentModule module = context.getBean(JsonComponentModule.class);
assertDeserialize(module); assertDeserialize(module);
context.close();
} }
@Test @Test
public void moduleShouldRegisterInnerClasses() throws Exception { public void moduleShouldRegisterInnerClasses() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( load(NameAndAgeJsonComponent.class);
JsonComponentModule.class, NameAndAgeJsonComponent.class); JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
JsonComponentModule module = context.getBean(JsonComponentModule.class);
assertSerialize(module); assertSerialize(module);
assertDeserialize(module); assertDeserialize(module);
context.close();
} }
@Test @Test
@ -68,6 +72,14 @@ public class JsonComponentModuleTests {
context.close(); context.close();
} }
private void load(Class<?>... configs) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(configs);
ctx.register(JsonComponentModule.class);
ctx.refresh();
this.context = ctx;
}
private void assertSerialize(Module module) throws Exception { private void assertSerialize(Module module) throws Exception {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module); mapper.registerModule(module);

Loading…
Cancel
Save