2016-03-01 78 views
2

我想测试我的序列化器,它将我的java对象解析为一个json对象。这是我的序列化器类:如何为定制Jackson序列化程序编写JUnit测试?

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 
     } 

因此,如何能测试我的串行器?我尝试了类似问题的其他答案,但没有为我工作。

我这样使用串行我在其他clases:

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

也许你应该嘲笑JsonGenerator和验证是否写入正确的内容。 – andrucz

回答

0

好的,谢谢你的答案。我现在明白了这种方式,它工作正常:

我改变了我的串行一点点:

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

事情是这样的:

@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("{}"); 
} 
+0

为什么downvote?现在我很难过:-( –

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\" }");