2

コードの JUnit テストを作成しようとしています。しかし問題は、ICompilationUnit、IPackageFragment、ITypes などの Java モデルを使用していることです。ICompilationUnit を作成してテストする方法がわかりませんでした。google と 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();
            }
        }
    }
}
4

1 に答える 1

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);
于 2012-08-19T14:37:22.277 に答える