如何对自定义Jackson JsonSerializer进行单元测试?

时间:2015-11-24 11:42:59

标签: java json junit jackson jsonserializer

我写了以下JsonSerializerJackson将整数数组序列化为JSON:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;

public class TalkIdsSerializer extends JsonSerializer<TalkIds> {

    /**
     * Serializes a TalkIds object into the following JSON string:
     * Example: { "talk_ids" : [ 5931, 5930 ] }
     */
    @Override
    public void serialize(TalkIds talkIds, JsonGenerator jsonGenerator, 
        SerializerProvider provider)
            throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeArrayFieldStart(TalkIds.API_DICTIONARY_KEY);
        for (Integer talkId : talkIds.getTalkIds()) {
            jsonGenerator.writeNumber(talkId);
        }
        jsonGenerator.writeEndArray();
        jsonGenerator.writeEndObject();
    }

}

这里使用的是:

@JsonSerialize(using = TalkIdsSerializer.class)
public class TalkIds { /* ...  */ }

我想测试序列化程序的行为并提出以下内容:

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.junit.Before;
import org.junit.Test;    
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class TalkIdsSerializerTest {

    protected final ArrayList<Integer> TALK_IDS = 
        new ArrayList<>(Arrays.asList(5931, 5930));

    protected TalkIdsSerializer talkIdsSerializer;

    @Before
    public void setup() throws IOException {
        talkIdsSerializer = new TalkIdsSerializer();
    }

    @Test
    public void testSerialize() throws IOException {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator jsonGenerator = 
            new JsonFactory().createGenerator(stringWriter);
        TalkIds talkIds = new TalkIds();
        talkIds.add(TALK_IDS);
        talkIdsSerializer.serialize(talkIds, jsonGenerator, null);
        String string = stringWriter.toString(); // string is ""
        assertNotNull(string);
        assertTrue(string.length() > 0);
        stringWriter.close();
    }

}

然而,没有会写入StringWriter。我做错了什么?

3 个答案:

答案 0 :(得分:5)

您需要flush()生成器

  

调用方法将任何缓冲的内容刷新到底层目标(输出流,编写器),并刷新目标本身。
  http://fasterxml.github.io/jackson-core/javadoc/2.1.0/com/fasterxml/jackson/core/JsonGenerator.html#flush()

答案 1 :(得分:2)

我有类似的要求,要测试自定义序列化程序。我使用objectMapper直接获取字符串(因为你已经用JsonSerialize注释了TalkIds)。您可以从对象获取json字符串,如下所示

$tree = array_merge($tree, $this->build_tree($data, $value->id));

答案 2 :(得分:0)

对我来说flush()没有改变任何内容,所以我根据http://www.baeldung.com/jackson-custom-serialization改变了测试方法。

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.StringWriter;

//...

    @Test
    public void serialize_custom() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addSerializer(MyCustomSerializer.class, myCustomSerializer);
        objectMapper.registerModule(module);
        StringWriter stringWriter = new StringWriter();

        TalkIds talkIds = new TalkIds();
        talkIds.add(TALK_IDS);

        objectMapper.writeValue(stringWriter,wi);
        assertTrue(stringWriter.toString().length() > 3);
    }