2

I am trying to make some JUnit tests for my code. But the problem is, I make use of the Java Models like ICompilationUnit, IPackageFragment, ITypes and etc. I did not get how to create some ICompilationUnit and then test. I searched google and stackoverflow for information but did not find something.

My question is, how can I make Junit test with classes of the jdt.core...can somebody give me may be some code examples.

Thanks

Here is a Method I coded:

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();
            }
        }
    }
}
Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
malib5
  • 59
  • 1
  • 7
  • Can you show an example of a method you want to test and maybe your attempt at writing a test for it? – Don Roby Aug 19 '12 at 12:26

1 Answers1

1

This snippet of code shows how to take some Java source (in variable javaSource) and get an ICompilationUnit from it by using the Java AST parser. You get these classes by using plugin org.eclipse.jdt.core as a dependency.

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);
Chris Gerken
  • 16,221
  • 6
  • 44
  • 59