使用 Jackson 和 Mockito 序列化对象时的无限递归

2022-09-04 21:11:37

在使用 MockMvc、Mockito 和 Jackson 测试 Spring 控制器时,我遇到了这个问题,所以我做了一个简单的类来测试 Jackson 的行为。我使用的是 jackson-databind:2.3.1 和 mockito-core:1.9.5。

给定此类:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.Serializable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class Person implements Serializable {
    private String name;
    private int age;

    // Public getters and setters...

    public static void main(String[] args) {
        String name = "Bob";
        int age = 21;
        ObjectMapper objectMapper = new ObjectMapper();

        // attempt serialization with real object
        Person person = new Person();
        person.setName(name);
        person.setAge(age);
        try {
            System.out.println(objectMapper.writeValueAsString(person));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            System.err.println("Failed to serialize real object");
        }

        // attempt serialization with mock object
        Person mockPerson = mock(Person.class);
        when(mockPerson.getName()).thenReturn(name);
        when(mockPerson.getAge()).thenReturn(age);
        try {
            System.out.println(objectMapper.writeValueAsString(mockPerson));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            System.err.println("Failed to serialize mock object.");
        }
    }

Jackson 对序列化真实对象没有问题,但是当它尝试序列化模拟对象时,它会抛出 JsonMappingException。通过代码进行调试,它反复调用serializeFields(bean,jgen,provider),卡在内部Mockito属性上。

所以,我的问题是:有没有办法强迫杰克逊使用getter方法?我尝试在类上@JsonIgnoreProperties,在字段上@JsonIgnore,在方法上@JsonProperty(以不同的组合,但没有成功)。或者,我是否必须编写自己的自定义序列化程序?

谢谢!


答案 1

以下是适用于特定情况的解决方案:

首先,您需要创建一个PersonMixin,因为您无法将所需的注释添加到模拟中。

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;


@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE)
public interface PersonMixin {

    @JsonProperty
    String getName();

    @JsonProperty
    Integer getAge();
}

现在,像下面的代码中那样使用对象映射器,您将获得与序列化真实对象时相同的结果:

Person mockPerson = mock(Person.class);
when(mockPerson.getName()).thenReturn(name);
when(mockPerson.getAge()).thenReturn(age);
objectMapper.addMixInAnnotations(Person.class, PersonMixin.class);
try {
    System.out.println(objectMapper.writeValueAsString(mockPerson));
} catch (JsonProcessingException e) {
    e.printStackTrace();
    System.err.println("Failed to serialize mock object.");
}

答案 2

这是我在不需要mixins的情况下整理出来的。ObjectMapper

映射器会忽略其名称中某个位置有“Mockito”的所有成员。

此解决方案避免了对每个序列化对象进行混音,或对可能无法访问的代码进行批注。

运行以下测试成功,并显示输出 。{"name":"Jonh"}

package test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import org.mockito.Mockito;

public class AppTest extends Mockito {

    public void testApp() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {

            @Override
            public boolean hasIgnoreMarker(final AnnotatedMember m) {
                return super.hasIgnoreMarker(m) || m.getName().contains("Mockito");
            }
        });

        final String name = "Jonh";

        Person mockPerson = mock(Person.class);
        when(mockPerson.getName()).thenReturn(name);
        System.out.println(mapper.writeValueAsString(mockPerson));
    }


    public static class Person {

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}