我正在尝试为我的代码进行一些 JUnit 测试。但问题是,我使用了 ICompilationUnit、IPackageFragment、ITypes 等 Java 模型。我不知道如何创建一些 ICompilationUnit 然后进行测试。我搜索了 google 和 stackoverflow 来获取信息,但没有找到任何东西。
我的问题是,如何使用 jdt.core 的类进行 Junit 测试...有人能给我一些代码示例吗?
Thanks
这是我编码的方法:
private void updateLists() {
if(!getCompilationUnit().isEmpty()){
for(int i = 0; i < getCompilationUnit().size(); i++){
try {
Document doc = new Document(getCompilationUnit().get(i).getSource());
int totalNumberOfCode = doc.getNumberOfLines();
IType type = getCompilationUnit().get(i).findPrimaryType();
IType[] types = getCompilationUnit().get(i).getTypes();
updateListPrimaryType(type, totalNumberOfCode, types);
updateListIMethod(type);
updateListMember(type,types);
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
此代码片段展示了如何使用 Java AST 解析器获取一些 Java 源代码(在变量 javaSource 中)并从中获取 ICompilationUnit。您可以通过使用插件 org.eclipse.jdt.core 作为依赖项来获取这些类。
import org.eclipse.jdt.core.dom.ASTParser;
String javaSource = "some java source"'
// see org.eclipse.jdt.core.dom.AST for a complete
// list of supported levels.
ASTParser parser = ASTParser.newParser(AST.JLS2);
parser.setSource(javaSource.toCharArray());
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)