Class JUnitMatchers

java.lang.Object
com.google.errorprone.matchers.JUnitMatchers

public final class JUnitMatchers
extends Object
Matchers for code patterns which appear to be JUnit-based tests.
Author:
alexeagle@google.com (Alex Eagle), eaftan@google.com (Eddie Aftandillian)
  • Field Details

    • JUNIT4_TEST_ANNOTATION

      public static final String JUNIT4_TEST_ANNOTATION
      See Also:
      Constant Field Values
    • JUNIT4_THEORY_ANNOTATION

      public static final String JUNIT4_THEORY_ANNOTATION
      See Also:
      Constant Field Values
    • JUNIT_BEFORE_ANNOTATION

      public static final String JUNIT_BEFORE_ANNOTATION
      See Also:
      Constant Field Values
    • JUNIT_AFTER_ANNOTATION

      public static final String JUNIT_AFTER_ANNOTATION
      See Also:
      Constant Field Values
    • JUNIT_BEFORE_CLASS_ANNOTATION

      public static final String JUNIT_BEFORE_CLASS_ANNOTATION
      See Also:
      Constant Field Values
    • JUNIT_AFTER_CLASS_ANNOTATION

      public static final String JUNIT_AFTER_CLASS_ANNOTATION
      See Also:
      Constant Field Values
    • JUNIT4_RUN_WITH_ANNOTATION

      public static final String JUNIT4_RUN_WITH_ANNOTATION
      See Also:
      Constant Field Values
    • hasJUnit4BeforeAnnotations

      public static final Matcher<com.sun.source.tree.MethodTree> hasJUnit4BeforeAnnotations
    • hasJUnit4AfterAnnotations

      public static final Matcher<com.sun.source.tree.MethodTree> hasJUnit4AfterAnnotations
    • isTestCaseDescendant

      public static final Matcher<com.sun.source.tree.ClassTree> isTestCaseDescendant
      Matches a class that inherits from TestCase.
    • isConcreteClassWithoutRunWith

      public static final Matcher<com.sun.source.tree.ClassTree> isConcreteClassWithoutRunWith
      Match a class which appears to be missing a @RunWith annotation.

      Matches if: 1) The class does not have a JUnit 4 @RunWith annotation. 2) The class is concrete. 3) The class is a top-level class.

    • hasJUnit4TestCases

      public static final Matcher<com.sun.source.tree.ClassTree> hasJUnit4TestCases
      Match a class which has one or more methods with a JUnit 4 @Test annotation.
    • isJUnit3TestClass

      public static final Matcher<com.sun.source.tree.ClassTree> isJUnit3TestClass
      Match a class which appears to be a JUnit 3 test class.

      Matches if: 1) The class does inherit from TestCase. 2) The class does not have a JUnit 4 @RunWith annotation nor any methods annotated @Test. 3) The class is concrete. 4) This class is a top-level class.

    • isJunit3TestCase

      public static final Matcher<com.sun.source.tree.MethodTree> isJunit3TestCase
      Match a method which appears to be a JUnit 3 test case.

      Matches if: 1) The method's name begins with "test". 2) The method has no parameters. 3) The method is public. 4) The method returns void

    • looksLikeJUnit3SetUp

      public static final Matcher<com.sun.source.tree.MethodTree> looksLikeJUnit3SetUp
      Match a method which appears to be a JUnit 3 setUp method

      Matches if: 1) The method is named "setUp" 2) The method has no parameters 3) The method is a public or protected instance method that is not abstract 4) The method returns void

    • looksLikeJUnit4Before

      public static final Matcher<com.sun.source.tree.MethodTree> looksLikeJUnit4Before
      Matches a method which appears to be a JUnit4 @Before method.

      Matches if: 1) The method is annotated Before 2) The method has no parameters 3) The method is a public or protected instance method that is not abstract 4) The method returns void

    • looksLikeJUnit3TearDown

      public static final Matcher<com.sun.source.tree.MethodTree> looksLikeJUnit3TearDown
      Match a method which appears to be a JUnit 3 tearDown method

      Matches if: 1) The method is named "tearDown" 2) The method has no parameters 3) The method is a public or protected instance method that is not abstract 4) The method returns void

    • looksLikeJUnit4After

      public static final Matcher<com.sun.source.tree.MethodTree> looksLikeJUnit4After
      Matches a method which appears to be a JUnit4 @After method.

      Matches if: 1) The method is annotated After 2) The method has no parameters 3) The method is a public or protected instance method that is not abstract 4) The method returns void

    • wouldRunInJUnit4

      public static final Matcher<com.sun.source.tree.MethodTree> wouldRunInJUnit4
      Matches a method annotated with @Test but not @Ignore.
    • TEST_CASE

      public static final Matcher<com.sun.source.tree.MethodTree> TEST_CASE
      Matches a JUnit 3 or 4 test case.
    • hasJUnit4TestRunner

      public static final MultiMatcher<com.sun.source.tree.ClassTree,​com.sun.source.tree.AnnotationTree> hasJUnit4TestRunner
    • isJUnit4TestClass

      public static final Matcher<com.sun.source.tree.ClassTree> isJUnit4TestClass
      Matches classes which have attributes of only JUnit4 test classes.

      Matches if 1) the class is non-abstract, 2) the class does not inherit from JUnit3 TestCase, and 3) the class is annotated with @RunWith or any method therein is annotated with @Test.

    • isAmbiguousJUnitVersion

      public static final Matcher<com.sun.source.tree.ClassTree> isAmbiguousJUnitVersion
      Matches classes which have attributes of both JUnit 3 and 4 classes.

      Matches if the class 1) inherits from JUnit 3 TestCase, and 2) a) has a JUnit4 test runner annotation, or b) has any methods annotated @Test.

      As currently implemented, classes with ambiguous version will match neither isJUnit4TestClass nor isJUnit3TestClass.

  • Method Details

    • hasJUnitAnnotation

      public static boolean hasJUnitAnnotation​(com.sun.source.tree.MethodTree tree, VisitorState state)
      Checks if a method, or any overridden method, is annotated with any annotation from the org.junit package.
    • isJUnit4TestRunnerOfType

      public static Matcher<com.sun.source.tree.ExpressionTree> isJUnit4TestRunnerOfType​(Iterable<String> runnerTypes)
      Matches an argument of type Class<T>, where T is a subtype of one of the test runners listed in the TEST_RUNNERS field.

      TODO(eaftan): Support checking for an annotation that tells us whether this test runner expects tests to be annotated with @Test.

    • containsTestMethod

      public static boolean containsTestMethod​(com.sun.source.tree.Tree tree)
      Returns true if the tree contains a method invocation that looks like a test assertion.