我是Junit的初学者,我无法测试这些方法,所以有人能帮我摆脱这种情况吗 公共字符串序列化(映射数据)引发IOException{ debug(“开始序列化线程id[{}]和字符串[{}]的json响应”, Thread.currentThread().getId(),数据); return(data==null)?“{}”:新的ObjectMapper().writeValueAsString(data); }
要测试该方法,需要创建一个合理的输入值,然后调用该方法,然后检查输出值。在您的例子中,输入值是一个我是Junit的初学者,我无法测试这些方法,所以有人能帮我摆脱这种情况吗 公共字符串序列化(映射数据)引发IOException{ debug(“开始序列化线程id[{}]和字符串[{}]的json响应”, Thread.currentThread().getId(),数据); return(data==null)?“{}”:新的ObjectMapper().writeValueAsString(data); },junit,Junit,要测试该方法,需要创建一个合理的输入值,然后调用该方法,然后检查输出值。在您的例子中,输入值是一个映射,输出是一些字符串。为了让这一切顺利进行,你可以这样写: public String serialize(Map<String, Object> data) throws IOException { logger.debug("Starting serialisation of json response for thread id [{}] and string [{}]"
映射
,输出是一些字符串
。为了让这一切顺利进行,你可以这样写:
public String serialize(Map<String, Object> data) throws IOException {
logger.debug("Starting serialisation of json response for thread id [{}] and string [{}]",
Thread.currentThread().getId(), data);
return (data == null) ? "{}" : new ObjectMapper().writeValueAsString(data);
}
@测试
公共void testMyMethod(){
//首先,创建要测试的对象
MyObject aor=新的MyObject();
//现在创建将提供给该方法的映射
Map inputMap=newhashmap();
//将正确的数据值放入地图
对象数据=???
inputMap.put(Thread.currentThread().getId(),数据);
//调用该方法
字符串结果=aor.serialize(inputMap);
//现在断言您得到的结果是您期望的结果
assertEquals(“这是我期望得到的”,结果);
}
然后你就可以了。创建一个输入(地图)。调用您的方法。检查它是否返回预期的输出。这是你能写的最简单的测试。由于您有一个空输入的特殊情况,请添加另一个测试,检查您在输入为空时是否获得了预期的输出。实际上,我不知道此方法必须使用什么测试方法,请阅读以下内容:
@Test
public void testMyMethod() {
// First, create the object you're testing
MyObject aor = new MyObject();
// Now create the Map you'll be providing to the method
Map<String, Object> inputMap = new HashMap<>();
// Put the right data value into the Map
Object data = ???
inputMap.put(Thread.currentThread().getId(), data);
// Invoke the method
String result = aor.serialize(inputMap);
// Now assert that the result you got is the result you expected
assertEquals("this is what I expected to get", result);
}