小编Dan*_*ple的帖子

Jackson ObjectMapper使用自定义序列化器和反序列化器

我有一个配置Jackson ObjectMapper的类.它为我的对象类型添加了一些自定义序列化器和反序列化器,如下所示:

public class JsonMapperFactory {
    public static ObjectMapper createObjectMapper() {
        final SimpleModule module = new SimpleModule("customerSerializationModule", new Version(1, 0, 0, "static version"));
        addCustomDeserializersTo(module);
        addCustomSerializersTo(module);

        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(module);
        return objectMapper;
    }
    private static void addCustomSerializersTo(final SimpleModule module) {
        module.addSerializer(DateTime.class, new DateTimeSerializer());
    }
    private static void addCustomDeserializersTo(final SimpleModule objectMapper) {
        objectMapper.addDeserializer(DateTime.class, new DateTimeDeserializer());
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经在他们自己的测试类中测试了我的客户序列化器,所以在我对这个JsonMapperFactory类的测试中,我试图简单地检查创建的ObjectMapper是否具有预期的序列化器(或反序列化器)这可以通过内省ObjectMapper来实现,但是它似乎没有任何机制来做到这一点.

有谁知道测试它的好方法?

对于反序列化器,我有以下内容:

private void assertThatObjectMapperUsesCorrectDeserializer(final Class<?> typeClazz, final Class<?> deserializerClazz) throws JsonMappingException {
    final  DeserializationConfig deserializationConfig = this.objectMapper.getDeserializationConfig();
    final …
Run Code Online (Sandbox Code Playgroud)

java testing serialization jackson deserialization

11
推荐指数
1
解决办法
2万
查看次数

如何使用EasyMock模拟方法

User getUser(int id) {
    User user = service.get( id );
    if( user != null ) {
        user.Stuff = getUserStuff( id );
        return User;
    }

    throw new NotFoundException();
}

Stuff getUserStuff( int id ) {
    stuffGetter.getStuff( id ); // stuff getter makes a rest call
    return Stuff;
}
Run Code Online (Sandbox Code Playgroud)

使用EasyMock,我将如何测试该getUser(id)方法.对我来说令人困惑的部分是getUserStuff(id)进行外部调用,我不想在测试时做出来getUser.

java unit-testing easymock mocking

6
推荐指数
1
解决办法
9131
查看次数

方法是不返回String?

为Java做一些初学者问题:

给定两个字符串,将它们附加在一起(称为"连接")并返回结果.

但是,如果连接创建了一个双字符,则省略其中一个字符,因此"abc"和"cat"会产生"abcat".

我的代码:

public static String conCat(String a, String b) {
    //abc (c) == cat (c)
    if (a.substring(a.length()-1,a.length()) == b.substring(0,1)) {

        //return (ab) + (cat)
        return a.substring(0,a.length()-2) + b;

    //cat (c) == abc (c)
    } else if(b.substring(0,1) == a.substring(a.length()-1,a.length())) {

        //return (abc) + (at)
        return a + b.substring(2,b.length());

    } else if (a.equals("") || b.equals("")) {
        return a + b;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么Eclipse无法识别String返回.

java eclipse string return

0
推荐指数
1
解决办法
305
查看次数

将数字转换为单词

我想得到一些关于将整数转换为单词的帮助.我是Java的新手,我有点想到我将要做的事情,但需要一些帮助.

代码应打印0-999的数字,并在扫描仪中输入-1,程序应停止.

像这样:

请输入0到999之间的数字:1

请输入0到999之间的数字:11

十一

请输入0到999之间的数字:122

一百二十二

请输入0到999之间的数字:1000

数量超出范围

请输入0到999之间的数字:-1

感谢您使用我们的计划

我还必须使用方法来使这个"清洁"

java jgrasp

-6
推荐指数
2
解决办法
5万
查看次数