2017-03-13 8 views
1

저는 스프링 부트 프로젝트로 직렬화/직렬화를 위해 Jackson을 사용합니다. 나는 다음과 같은 구조의 DTO 개체가유닛 테스트를 위해 Jackon JsonProperty 액세스를 무시합니다.

, 다음과 같은 방법으로 내 단위 테스트에서이 개체의

public class TestDTO implements Serializable { 
    private static final long serialVersionUID = 1L; 

    private Long id; 

    @JsonProperty(access = JsonProperty.Access.READ_ONLY) 
    private UUID certificateId; 

    @NotNull 
    private Long orgId; 

    @NotNull 
    private CertificateType certificateType; 

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) 
    @Valid 
    @NotNull 
    private PublicCertificateDTO publicCertificate; 

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) 
    @Valid 
    private PrivateCertificateDTO privateCertificate; 

    @JsonProperty(access = JsonProperty.Access.READ_ONLY) 
    private ZonedDateTime expiryDate; 

    @JsonProperty(access = JsonProperty.Access.READ_ONLY) 
    private ZonedDateTime createdDate; 

    @JsonProperty(access = JsonProperty.Access.READ_ONLY) 
    private ZonedDateTime updatedDate; 
} 

직렬화,

public static byte[] convertObjectToJsonBytes(TestDTO object) 
     throws IOException { 
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 

    JavaTimeModule module = new JavaTimeModule(); 
    mapper.registerModule(module); 

    return mapper.writeValueAsBytes(object); 
} 

WRITE_ONLY 액세스 할 수있는 필드는 무시 얻을됩니다 (명백한 이유로). 따라서 직렬화 된 객체에서 null 값은 publicCertificateprivateCertificate입니다.

나는 mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)

단위 테스트에 대한 이러한 속성을 무시하는 다른 방법이 있나요 설정하려고 했습니까?

답변

1

지정된 솔루션이 작동하는 동안 요구 사항이 지나치게 많습니다. 주석을 모두 재정의하려는 경우 사용자 정의 직렬 변환기가 필요하지 않습니다. 당신이 @JsonProperty 주석을 무시하려는 경우

public class TestDTO 
{ 
    public String regularAccessProperty; 
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) 
    public String writeAccessProperty; 
} 

, 당신은 동일한 이름의 변수가있는 다른 POJO를 작성 (잭슨은 사소한 요구 사항

에 대한 mixin feature을 다음과 같은 간단한 POJO를 고려있다 또는 같은 게터/세터 이름) :

// mixin class that overrides json access annotation 
public class UnitTestDTO 
{ 
    @JsonProperty(access = JsonProperty.Access.READ_WRITE) 
    public String writeAccessProperty; 
} 

당신은 Simplemodule를 통해 원래의 POJO와 믹스 인을 연결 :

simpleModule.setMixInAnnotation(TestDTO.class, UnitTestDTO.class); 
+0

덜 귀찮은 솔루션으로 표시했습니다. – nixgadgets

0

이 문제는 JUnit 테스트를 위해 사용자 지정 serializer를 추가하여 해결되었습니다.

따라서 TestDTO에 대해 아래와 같이 시리얼 라이저를 추가했습니다.

private class TestJsonSerializer extends StdSerializer<TestDTO> { 
    public TestJsonSerializer() { 
     this(null); 
    } 

    public TestJsonSerializer(Class<TestDTO> t) { 
     super(t); 
    } 

    @Override 
    public void serialize(TestDTO value, JsonGenerator gen, SerializerProvider provider) throws IOException { 
     gen.writeStartObject(); 
     gen.writeNumberField("orgId", value.getOrgId()); 
     gen.writeStringField("certificateType", value.getCertificateType().getType()); 
     if (value.getPublicCertificate() != null) { 
      gen.writeObjectField("publicCertificate", value.getPublicCertificate()); 
     } 
     if (value.getPrivateCertificate() != null) { 
      gen.writeObjectField("privateCertificate", value.getPrivateCertificate()); 
     } 
     gen.writeObjectField("expiryDate", value.getExpiryDate()); 
     gen.writeObjectField("createdDate", value.getCreatedDate()); 
     gen.writeObjectField("updatedDate", value.getUpdatedDate()); 
     gen.writeEndObject(); 
    } 
} 

I 첨가가 중첩 된 객체

ObjectMapper mapper = new ObjectMapper(); 
SimpleModule simpleModule = new SimpleModule(); 
simpleModule.addSerializer(TestDTO.class, new TestJsonSerializer()); 
mapper.registerModule(simpleModule); 

마찬가지로 추가 등록 커스텀 시리얼, 및 publicCertificateprivateCertificate.