英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论