我是Junit的初学者,我无法测试这些方法,所以任何人都可以帮助我摆脱这种情况

时间:2016-04-29 11:31:01

标签: junit

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);
}

1 个答案:

答案 0 :(得分:0)

要测试该方法,可以创建一个合理的输入值,然后调用该方法,然后检查输出值。在您的情况下,输入值为Map<String, Object>,输出为String。所以为了使这个工作,你会写这样的东西:

@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);
}

然后你去。