如何为自定义Jackson Serializer编写JUnit-Test?

时间:2016-03-01 14:15:48

标签: java json serialization junit

我想测试一个将我的java对象解析为json对象的序列化程序。这是我的Serializer类:

public class CountryCodeSerializer extends JsonSerializer<CountryCode> {

    @Override
    public void serialize(CountryCode value, JsonGenerator generator, SerializerProvider provider)
            throws IOException, JsonProcessingException {

        if (value == null) {
            generator.writeString("{}");
        } else {
            generator.writeString(value.toString());
        }
    }

}

我的测试看起来像这样:

    @Before
        public void setUp() throws Exception {
            stringJson = new StringWriter();
            generator = new JsonFactory().createGenerator(stringJson);
            provider = new ObjectMapper().getSerializerProvider();
            countryCode = CountryCode.parse("us");
        }

        @Test
        public void parsingNullReturnsNull() throws Exception {
            assertThat(countryCodeSerializer.serialize(countryCode, generator, provider)).isEqualTo("{'countrycode':'us'}); //this doesn't work, since serialize() is void

//countryCodeSerializer.serialize(countryCode, generator, provider); //this throws an java.lang.NullPointerException
        }

那么如何测试我的序列化器呢?我尝试了类似问题的其他答案,但没有任何对我有用。

我在其他文章中使用这样的序列化器:

@JsonSerialize(using = CountryCodeSerializer.class)
    private CountryCode countryCode;

3 个答案:

答案 0 :(得分:1)

好的,谢谢你的答案。我现在用这种方式得到它并且工作正常:

我稍微更改了序列化程序:

public class CountryCodeSerializer extends JsonSerializer<CountryCode> {
       @Override
        public void serialize(CountryCode value, JsonGenerator generator, SerializerProvider provider)
                throws IOException, JsonProcessingException {

            if (null == value) {
                throw new IllegalArgumentException("CountryCode is null");
            } else {
                generator.writeString(value.toString());
            }
        }        
    }

这是我的两个测试:

public class CountryCodeSerializerTest {

    private CountryCodeSerializer countryCodeSerializer;
    private JsonGenerator jsonGenerator;

    @Before
    public void setUp() throws Exception {
        countryCodeSerializer = new CountryCodeSerializer();
        jsonGenerator = mock(JsonGenerator.class);
    }

    @Test
    public void testNullCountryCodeThrowsIllegalArgumentException() throws Exception {
        try {
            countryCodeSerializer.serialize(null, jsonGenerator, null);
            fail("An IllegalArgumentException should have been thrown.");
        } catch (IllegalArgumentException e) {
            //ok
        }
    }

    @Test
    public void testCountryCodeConvertedToJsonString() throws Exception {
        countryCodeSerializer.serialize(CountryCode.parse("us"), jsonGenerator, null);
        verify(jsonGenerator).writeString("us");
    }
}

答案 1 :(得分:0)

这样的事情:

@Mock 
private JsonGenerator generator;

@Test
public void testInstanceWithValue() {
    //SETUP
    String expectedValue = "test value";
    CountryCode value = mock(CountryCode.class);
    when(value.toString()).thenReturn(expectedValue);

    // CALL
    CountryCodeSerializer instance = new CountryCodeSerializer(value, generator, null);

    // VERIFY
    verify(generator).writeString(expectedValue);
}

@Test
public void testInstanceWithNull() {
    //SETUP
    CountryCode value = null;

    // CALL
    CountryCodeSerializer instance = new CountryCodeSerializer(value, generator, null);

    // VERIFY
    verify(generator).writeString("{}");
}

答案 2 :(得分:0)

这可以通过创建一个存储写入内容的自定义JsonGenerator来实现。

class TestJsonGenerator extends JsonGenerator {

    private StringBuilder stringBuilder = new StringBuilder();

    ...

    @Override
    public void writeString(String text) {
        stringBuilder.append(text);
    }

    public String getText() {
        return stringBuilder.toString();
    }

}

然后验证生成的内容,而无需检查所有对writeString的调用:

TestJsonGenerator testGenerator = new TestJsonGenerator();
serializer.serialize(countryCode, testGenerator, provider);

assertThat(testGenerator.getText()).isEqualsTo("{ \"foo\": \"bar\" }");