我在Ruby世界中经历了很长一段时间后回到Java中,我对JUnit测试以及我正在测试的源代码有疑问.
如果我有一个公司的图形代码包,我们可以调用它com.example.graphics,我是否应该将测试包含在该软件包中,还是应该包含在单独的软件包中com.example.graphics.test?
我想知道在Junit中处理受保护方法的好方法.
假设我想测试一个名为A的类,它有一个受保护的成员和构造函数.我明白为了测试类,AI应该编写另一个名为ATest的类,它可能扩展TestCase(这在Junit3中应该是强制的).因为我想测试一个受保护的方法,并且因为A有一个受保护的构造函数,我的测试类ATest也应该扩展实现该方法的类A,以便能够创建该类并访问该方法.
可能是两个类的双重继承一个很好的解决方案?
PS我已经知道在Junit 4中可以避免TestCase的继承.
下面是一个方法,我很难弄清楚如何使用JUnit进行测试.
这种方法很难测试,因为它取决于其他方法的结果(例如getClosestDcoumentCode).
基于我对JUnit的阅读,这表明我应该重构该方法.但是怎么样?如果不需要重构,您如何测试依赖于其他方法的方法?
谢谢,
埃利奥特
private static String findPrincipal(List<DocumentKey> documentkeys_) {
Hashtable<String, Integer> codecounts = new Hashtable<String, Integer>();
for (DocumentKey document : documentkeys_) {
int x = 0;
String closestCode = getClosestDocumentCode(document.candidates);
if (closestCode == null) continue;
int thecount = 0;
if (codecounts.containsKey(closestCode))
thecount = codecounts.get(closestCode);
if (document.hasKey)
thecount += 2;
else
thecount++;
codecounts.put(closestCode, new Integer(thecount));
x++;
}
String closestCode = getClosestCode(codecounts);
return closestCode;
}
Run Code Online (Sandbox Code Playgroud) java ×4
junit ×3
unit-testing ×2
junit3 ×1
junit4 ×1
packages ×1
refactoring ×1
tdd ×1
testing ×1