java - how do test with Junit for the method AddCalcNode()?
问题描述
Hello guys How do I test the method addCalcNode with JUnit?
public class Add {
private final ObjectMapper mapper = new ObjectMapper();
public ObjectNode addCalcNode(int i, int j) {
ObjectNode node = mapper.createObjectNode();
node.put("result", i+j);
return node;
}
}
解决方案
在日食中,
- 右键单击包含此类的文件(在左侧的文件夹视图中)。
New > Other
从上下文菜单中选择 - 在弹出窗口中,选择
Java > JUnit > JUnit Test Case
,然后单击“下一步”按钮 - 单击“完成”。如果需要,您可以在此处更改一些属性 - 它们相对简单 - 但在这种情况下没有太多需要。
现在,你有你的AddTest
课。是时候编写一个测试方法了addCalcNode()
:
public class AddTest {
@Test
public void addCalcNodeTest() {
// get a value from our class
Add myAdd = new Add();
ObjectNode addedNode = myAdd.addCalcNode(1, 2);
// test that that value is correct
// I don't know how your ObjectMapper works, so I'll just do this to demonstrate
ObjectNode expected = (new ObjectMapper()).createObjectNode();
expected.put("result", 3);
// the assert functions are the core of JUnit, for testing that your function does
// what you want it to. assertEquals() is the most basic of them.
assertEquals(expected, addedNode);
}
}
现在,右键单击这个 JUnit 类文件(在左侧的文件夹视图中),然后Run As > JUnit Test
从上下文菜单中选择。
测试是否通过或失败的显示应该出现在某处。这就是制作和运行基本 JUnit 测试的方法。您可以从那里扩展您的测试,或者为这个测试添加更多功能,或者进行更多测试(只是用 注释的方法@Test
)。
推荐阅读
- qt - 改变持有QDockWidgets的QTabBar的位置?
- azure - 将值从 Azure 逻辑应用程序传递到 HttpTrigger Azure 函数
- sql - 如何根据匹配的 ID 更新列
- ios - “用户被阻止直播。” (域=com.google.GTLRErrorObjectDomain 代码=403)
- c# - 从数据库获取结果时防止循环引用
- amazon-web-services - 如何在无服务器框架的“serverless.yml”文件中获取 URL 端点详细信息作为变量?
- perl - 用 Perl 读取 CSV 会产生扭曲的线条
- c++ - 无法在电子中获取 displayName
- python - 如何正确初始化 PDB.DSSP 对象
- html - 如何使用 Chart.js 和 Angular 框架在 html 画布中正确显示图表