自定义日期序列机的单元测试

发布于 2025-02-01 14:29:29 字数 948 浏览 3 评论 0 原文

我最近开始在Java编写测试,并且在以下课程的测试中遇到了一些问题。如何以这种形式正确编写测试testetimeserializer类。

public class TestDateTimeSerializer extends JsonSerializer<LocalDateTime> {
    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

    @Override
    public void serialize(LocalDateTime dt, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
        if (dt == null) {
            gen.writeNull();
        } else {
            gen.writeString(dt.format(FORMATTER));
        }
    }
}

此课程在这里使用

@Data
@JsonIgnoreProperties(ignoreUnknown = true) 
@JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
public class TestRequest implements ITestResponse {
    @JsonProperty("TestID")
    private Long testID;
    @JsonSerialize(using = TestDateTimeSerializer.class)
    private LocalDateTime lastUpdateDate;
}

I have recently started writing tests in Java and I am having some problem with the tests for the following class. How to write correctly tests for TestDateTimeSerializer class in such a form.

public class TestDateTimeSerializer extends JsonSerializer<LocalDateTime> {
    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

    @Override
    public void serialize(LocalDateTime dt, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
        if (dt == null) {
            gen.writeNull();
        } else {
            gen.writeString(dt.format(FORMATTER));
        }
    }
}

this class is used here

@Data
@JsonIgnoreProperties(ignoreUnknown = true) 
@JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
public class TestRequest implements ITestResponse {
    @JsonProperty("TestID")
    private Long testID;
    @JsonSerialize(using = TestDateTimeSerializer.class)
    private LocalDateTime lastUpdateDate;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼疼热情 2025-02-08 14:29:29

jsongenerator 作为模拟对象,然后验证其方法调用。

@ExtendWith(MockitoExtension.class)
class TestDateTimeSerializerTest {
    @Mock
    JsonGenerator generator;       // JsonGenerator is mock
    @Mock
    SerializerProvider provider;

    @Test
    void test() throws IOException {
        TestDateTimeSerializer serializer = new TestDateTimeSerializer();
        LocalDateTime now = LocalDateTime.now();
        
        // run your method
        serializer.serialize(now, generator, provider);

        // assert mocked generator's method call
        verify(generator).writeString(ArgumentMatchers.anyString());
    }
}

MOCKITO API doc:

您可以找到更多示例和用法。

Make JsonGenerator as mock object, then verify its method call.

@ExtendWith(MockitoExtension.class)
class TestDateTimeSerializerTest {
    @Mock
    JsonGenerator generator;       // JsonGenerator is mock
    @Mock
    SerializerProvider provider;

    @Test
    void test() throws IOException {
        TestDateTimeSerializer serializer = new TestDateTimeSerializer();
        LocalDateTime now = LocalDateTime.now();
        
        // run your method
        serializer.serialize(now, generator, provider);

        // assert mocked generator's method call
        verify(generator).writeString(ArgumentMatchers.anyString());
    }
}

Mockito API doc : https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#1

you can find more example and usage.

迷路的信 2025-02-08 14:29:29

请尝试以下操作:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.junit.Before;
import org.junit.Test;

import java.time.LocalDateTime;

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;
import static org.junit.Assert.*;

public class TestDateTimeSerializerSerializerTest {

        private ObjectMapper mapper;
        protected TestDateTimeSerializer serializer;

        @Before
        public void setup() {
            mapper = new ObjectMapper();
            serializer = new TestDateTimeSerializer();

            SimpleModule module = new SimpleModule();
            module.addSerializer(LocalDateTime.class, serializer);
            mapper.registerModule(module);
        }

        @Test
        public void testSerializeNonNull() throws JsonProcessingException {
            LocalDateTime ldt = LocalDateTime.parse("2016-10-06T10:40:21.124", ISO_LOCAL_DATE_TIME);

            String serialized = mapper.writeValueAsString(ldt);

            assertEquals("equal strings", "\"2016-10-06T10:40:21.124Z\"", serialized);
        }

        @Test
        public void testSerializeNull() throws JsonProcessingException {
            LocalDateTime ldt = null;

            String serialized = mapper.writeValueAsString(ldt);

            assertEquals("equal strings", "null", serialized);
        }

    }

Please try this:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.junit.Before;
import org.junit.Test;

import java.time.LocalDateTime;

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;
import static org.junit.Assert.*;

public class TestDateTimeSerializerSerializerTest {

        private ObjectMapper mapper;
        protected TestDateTimeSerializer serializer;

        @Before
        public void setup() {
            mapper = new ObjectMapper();
            serializer = new TestDateTimeSerializer();

            SimpleModule module = new SimpleModule();
            module.addSerializer(LocalDateTime.class, serializer);
            mapper.registerModule(module);
        }

        @Test
        public void testSerializeNonNull() throws JsonProcessingException {
            LocalDateTime ldt = LocalDateTime.parse("2016-10-06T10:40:21.124", ISO_LOCAL_DATE_TIME);

            String serialized = mapper.writeValueAsString(ldt);

            assertEquals("equal strings", "\"2016-10-06T10:40:21.124Z\"", serialized);
        }

        @Test
        public void testSerializeNull() throws JsonProcessingException {
            LocalDateTime ldt = null;

            String serialized = mapper.writeValueAsString(ldt);

            assertEquals("equal strings", "null", serialized);
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文