如何为返回类型为void的函数编写单元测试。

huangapple go评论89阅读模式
英文:

How do i write unit test for function with void return type

问题

我刚开始学习编写单元测试我看了一些 jUnit 的示例但它们都是针对返回值的函数

我需要为以下方法编写单元测试同时我们在这个方法中使用了不属于这个类的对象和方法比如 ConfigParser 和 ParseConfig我该如何为这种方法编写单元测试呢

@RequestMapping(value = "/generate-json", consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
@PageType(pageType = "PlaceHolderPageType")
public void execute(@RequestBody String payload) throws JSONException {
    JSONObject json = new JSONObject(payload);
    JSONObject configJSON = generateJSONSchema(json.toString());
    String capabilityName = (String) configJSON.get(JSONConfigurationConstants.CAPABILITY_NAME);
    String fileLocation = FormInputFieldConstants.JSON_PATH + capabilityName + JSONConfigurationConstants.FILE_EXTENSION;
    //Write JSON file
    try (OutputStreamWriter file = new OutputStreamWriter(new FileOutputStream(fileLocation), StandardCharsets.UTF_8)) {
        file.write(configJSON.toString());
        file.flush();
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    URI uriSchemaLocation = new File(fileLocation).toURI();
    ConfigParser configParser = new ConfigParser(uriSchemaLocation);
    configParser.parseConfig(TestHelper.getMockedJSONObject(fileLocation));
}
英文:

I am new to writing Unit tests. I saw few examples of jUnit , but they all were for functions returning a value.

I have to write unit test for following method. Also we are using objects and methods in this method not belonging to this class like ConfigParser and ParseConfig. How do i go about writing Unit tests for such menthods?

    @RequestMapping(value = "/generate-json" , consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
@PageType(pageType = "PlaceHolderPageType")
public void execute(@RequestBody String payload) throws JSONException {
    JSONObject json = new JSONObject(payload);
    JSONObject configJSON = generateJSONSchema(json.toString());
    String capabilityName = (String) configJSON.get(JSONConfigurationConstants.CAPABILITY_NAME);
    String fileLocation = FormInputFieldConstants.JSON_PATH + capabilityName + JSONConfigurationConstants.FILE_EXTENSION;
    //Write JSON file
    try (OutputStreamWriter file  = new OutputStreamWriter(new FileOutputStream(fileLocation), StandardCharsets.UTF_8)) {
        file.write(configJSON.toString());
        file.flush();
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    URI uriSchemaLocation = new File(fileLocation).toURI();
    ConfigParser configParser = new ConfigParser(uriSchemaLocation);
    configParser.parseConfig(TestHelper.getMockedJSONObject(fileLocation));
}

答案1

得分: 1

这是您打算测试的方法,对吗?我很惊讶地看到它包含测试代码(configParser.parseConfig(TestHelper.getMockedJSONObject(fileLocation));)...

您到底想要测试什么?

  • 您可以例如通过加载写入的文件来验证文件的内容。然而,这只考虑了方法逻辑的一部分。

  • 我没有看到任何 @Override 注解,因此我认为您没有在重写方法。您可以更改方法签名以返回解析后的配置,并使用断言进行检查。

  • 可能有其他方法来获取配置;这取决于 configParser.parseConfig(...) 的逻辑。

  • 您还可以考虑将最后三行提取到另一个方法中并测试该方法。

总之,您可以更改方法以返回结果,或者将代码块提取到自己的方法中并对其进行测试,或者从另一个源中检索结果(如果类似于 configParser.getParsedConfig() 的内容存在)。

英文:

This is the method that you intend to test, correct? I am surprised to see that it contains test code (configParser.parseConfig(TestHelper.getMockedJSONObject(fileLocation));)...

What exactly do you want to test?

  • You could for example verify the content of the file that gets written by loading that file. However, that accounts only for a part of the method logic.

  • I don't see any @Override annotation and therefore assume that you are not overriding a method. You could change the method signature to return the parsed config and check it with assertions.

  • There might be other ways to retrieve the configuration; this depends on the logic of configParser.parseConfig(...).

  • You could also consider extracting the last tree lines to another method and test that method.

To sum up, you can either change the method to return a result, or extract chunks to own methods and test these, or retrieve the result from another source (in case something like configParser.getParsedConfig() exists).

huangapple
  • 本文由 发表于 2020年5月29日 15:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/62080899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定