java - 尝试进行 JUnit 测试但遇到一些错误
问题描述
下面是 JUnit 测试器类
import java.lang.reflect.Method;
public class SimpleUnitTester {
public int execute(Class clazz) throws Exception {
int failedCount = 0;
// your code
execute(Reflection.class);
Reflection reflection = null;
try {
reflection = (Reflection) clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().startsWith("test") && method.getReturnType() == boolean.class) {
try {
boolean returnedValue = ((Boolean) method.invoke(reflection)).booleanValue();
if (!returnedValue) {
failedCount++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return failedCount;
}
}
下面是反射类
public class Reflection {
public void go() {
}
public boolean testA() {
return true;
}
public void foo() {
}
public void bar() {
}
public int testB() {
return 0;
}
public boolean testC() {
return false;
}
public boolean testD() {
return false;
}
public boolean testE() {
return true;
}
public boolean anotherTest() {
return false;
}
}
我想做类似的测试,在反射类中,如果方法以关键字“test”开头并且如果方法具有布尔返回类型,那么它将使测试合格。如果测试失败,则返回测试失败值。但是我在 Output 中得到了 Null。
解决方案
推荐阅读
- ffmpeg - 在 ffmpeg 中使用自定义 io 复用编码器输出
- html - 在Angular中使用overflow-y滚动到容器内的div
- xamarin - Xamarin 自定义计划视图?
- notifications - 获取推送订阅端点 URL 时,我可以只使用 PHP 将通知负载发布到端点 URL 吗?
- javascript - 如何重定向到带有密码的页面,但无法通过 URL 访问该页面?
- sql - 从另一个表中插入值并使用返回值更新原始表
- python - 如何将 pd.DataFrame.rolling.corr() 与 Kendall 的 Tau 或 Spearman 一起使用
- flutter - 我是否总是必须等待“https://google.bintray.com”在停机维护后才能完全运行,然后才能构建我的颤振应用程序?
- node.js - AWS Fargate 服务中的“导致执行用户进程:执行格式错误”
- git - 为本地浅拷贝切换 git 标签/分支