javajunitgrep

How to find jUnit tests in a Java project?


I have a Java project with a lot of classes. Now I want to get a list with all jUnit tests. My idea was to use grep for that.

So I navigated to the root folder, and use the following command:

grep junit -R ./ > output.txt

But obviously this isn't correct. So my question is. How is the correct command? And are there ways that are more easier to find jUnit tests?


Solution

  • You'd want:

    grep -r junit *
    

    But you need to be a little careful with that, as it'll go through all files, include jars.

    Better to use find or ack:

    ack --java -cl '@Test' # Print filenames with, and # occurences of, @Test annotations
    

    Or more boringly:

    find . -name *.java | xargs grep junit
    ack --java junit # Or, more accurately:
    ack --java 'import\s.*?junit.*'