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:
    • JUNIT4_THEORY_ANNOTATION

      public static final String JUNIT4_THEORY_ANNOTATION
      See Also:
    • JUNIT_BEFORE_ANNOTATION

      public static final String JUNIT_BEFORE_ANNOTATION
      See Also:
    • JUNIT_AFTER_ANNOTATION

      public static final String JUNIT_AFTER_ANNOTATION
      See Also:
    • JUNIT_BEFORE_CLASS_ANNOTATION

      public static final String JUNIT_BEFORE_CLASS_ANNOTATION
      See Also:
    • JUNIT_AFTER_CLASS_ANNOTATION

      public static final String JUNIT_AFTER_CLASS_ANNOTATION
      See Also:
    • JUNIT4_RUN_WITH_ANNOTATION

      public static final String JUNIT4_RUN_WITH_ANNOTATION
      See Also:
    • JUNIT4_ASSERT_CLASS

      public static final String JUNIT4_ASSERT_CLASS
      See Also:
    • JUNIT3_TEST_CASE_CLASS

      public static final String JUNIT3_TEST_CASE_CLASS
      See Also:
    • JUNIT4_IGNORE_ANNOTATION

      public static final String JUNIT4_IGNORE_ANNOTATION
      See Also:
    • JUNIT3_SUPPRESS_ANNOTATION

      public static final String JUNIT3_SUPPRESS_ANNOTATION
      See Also:
    • JUNIT4_RUNNER_CLASS

      public static final String JUNIT4_RUNNER_CLASS
      See Also:
    • JUNIT3_ASSERT_CLASS

      public static final String JUNIT3_ASSERT_CLASS
      See Also:
    • hasJUnit4BeforeAnnotations

      public static final Matcher<MethodTree> hasJUnit4BeforeAnnotations
    • hasJUnit4AfterAnnotations

      public static final Matcher<MethodTree> hasJUnit4AfterAnnotations
    • isTestCaseDescendant

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

      public static final Matcher<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<ClassTree> hasJUnit4TestCases
      Match a class which has one or more methods with a JUnit 4 @Test annotation.
    • isJUnit3TestClass

      public static final Matcher<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<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<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<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<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<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<MethodTree> wouldRunInJUnit4
      Matches a method annotated with @Test but not @Ignore.
    • TEST_CASE

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

      public static final MultiMatcher<ClassTree, AnnotationTree> hasJUnit4TestRunner
    • isJUnit4TestClass

      public static final Matcher<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.
      3. The class is annotated with @RunWith or any method therein is annotated with @Test.
    • isAmbiguousJUnitVersion

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

      Matches if the class:

      1. Inherits from JUnit 3 TestCase.
        1. Has a JUnit4 test runner annotation, or
        2. 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(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<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.