Class TestParameterValuesProvider.Context

java.lang.Object
com.google.testing.junit.testparameterinjector.TestParameterValuesProvider.Context
Enclosing class:
TestParameterValuesProvider

public static final class TestParameterValuesProvider.Context extends Object
An immutable value class that contains extra information about the context of the parameter for which values are being provided.
  • Method Details

    • getOtherAnnotation

      public <A extends Annotation> A getOtherAnnotation(Class<A> annotationType)
      Returns the only annotation with the given type on the field or parameter that was annotated with @TestParameter.

      For example, if the test code is as follows:

         @Test
         public void myTest_success(
             @CustomAnnotation(123) @TestParameter(valuesProvider=MyProvider.class) Foo foo) {
           ...
         }
       
      then context.getOtherAnnotation(CustomAnnotation.class).value() will equal 123.
      Throws:
      NoSuchElementException - if this there is no annotation with the given type
      IllegalArgumentException - if there are multiple annotations with the given type
      IllegalArgumentException - if the argument is TestParameter.class because it is already handled by the TestParameterInjector framework.
    • getOtherAnnotations

      public <A extends Annotation> com.google.common.collect.ImmutableList<A> getOtherAnnotations(Class<A> annotationType)
      Returns all annotations with the given type on the field or parameter that was annotated with @TestParameter.

      For example, if the test code is as follows:

         @Test
         public void myTest_success(
             @CustomAnnotation(123)
             @CustomAnnotation(456)
             @TestParameter(valuesProvider=MyProvider.class)
             Foo foo) {
           ...
         }
       
      then context.getOtherAnnotations(CustomAnnotation.class) will return the annotations with 123 and 456.

      Returns an empty list if this there is no annotation with the given type.

      Throws:
      IllegalArgumentException - if the argument is TestParameter.class because it is already handled by the TestParameterInjector framework.
    • testClass

      public Class<?> testClass()
      The class that contains the test that is currently being run.

      Having this can be useful when sharing providers between tests that have the same base class. In those cases, an abstract method can be called as follows:

         ((MyBaseClass) context.testClass().newInstance()).myAbstractMethod()
       
    • toString

      public String toString()
      Overrides:
      toString in class Object