저는 스프링 부트 프로젝트로 직렬화/직렬화를 위해 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 값은 publicCertificate
과 privateCertificate
입니다.
나는 mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
단위 테스트에 대한 이러한 속성을 무시하는 다른 방법이 있나요 설정하려고 했습니까?
덜 귀찮은 솔루션으로 표시했습니다. – nixgadgets