对于jdt.core的Java模型JUnit测试(JUnit test for jdt.core J

2019-09-21 08:20发布

我试图让我的代码一些JUnit测试。 但问题是,我使用了Java模型,如ICompilationUnit,IPackageFragment,ITypes等的,我没有得到如何创建一些ICompilationUnit然后进行测试。 我搜索谷歌和计算器的信息,但没有找到。

我的问题是,如何才能让与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();
            }
        }
    }
}

Answer 1:

下面这段代码演示了如何采取一些Java源(可变的JavaSource),并通过使用Java AST解析器从它那里得到一个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);


文章来源: JUnit test for jdt.core Java Models