JUnit测试jdt.core Java模型

我正在尝试为我的代码进行一些JUnit测试。 但问题是,我使用了像DB2ompilationUnit,IPackageFragment,ITypes等Java模型。我没有得到如何创建一些ICompilationUnit然后测试。 我搜索谷歌和stackoverflow的信息,但没有找到的东西。

我的问题是,如何使用jdt.core的类进行Junit测试…有人可能会给我一些代码示例。

谢谢

这是我编码的方法:

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);