com.atlassian.webdriver.testing.rule
Class InjectionRules

java.lang.Object
  extended by com.atlassian.webdriver.testing.rule.InjectionRules

public final class InjectionRules
extends Object

Static factory for a set of JUnit4-compatible injection rules.

Similar to TestedProductRule, but more powerful, this set of rules allows for injection of components living in the context of a tested product or its associated page binder, into test classes and objects.

The product, or its page binder, MUST implement the InjectionContext interface, otherwise no injection will be performed.

To achieve both static and member injection (which also implies that a single tested product instance will be associated with test class, rather than each test object), combine ClassRule and @Rule in the following pattern:

     @ClassRule
     public static TestRule myProductStaticInjectionRule = InjectionRules.forTestClass(MyProduct.class);

     @Rule
     public TestRule myProductMemberInjectionRule = InjectionRules.forTestInContext(this);


     // (fields that will get injected)
     @Inject
     private static MyProduct myProduct; // per class

     @Inject
     private PageBinder myPageBinder; // per test object instance, but the same binder instance

     // etc...
 

You may also choose to use the class rule only, if all you need is static members injection.

If you prefer having a new tested product created for each test method, use the following factory in conjunction with Rule annotation:

     @Rule
     public TestRule myProductMemberInjectionRule = TestInjectionRule.forTest(this, MyProduct.class);

      // (fields that will get injected)

     @Inject
     private PageBinder myPageBinder; // per test object instance, but the same binder instance

     // etc...
 

An example test class

IMPORTANT: the methods forTestInContext(Object) and forTest(Object, Class) provide different implementations of rules, the first of which depends on a class rule created via forTestClass(Class) being present on the test class. Please choose one of the above options for your test (or base test) depending whether you need just static or just member injection, or both.

Since:
2.1

Method Summary
static
<T extends TestedProduct<?>>
org.junit.rules.TestRule
forTest(Object testInstance, Class<T> testedProductClass)
           
static
<T extends TestedProduct<?>>
org.junit.rules.TestRule
forTest(Object testInstance, com.google.common.base.Supplier<T> productSupplier)
           
static
<T extends TestedProduct<?>>
InjectingTestRule
forTestClass(Class<T> productClass)
           
static
<T extends TestedProduct<?>>
InjectingTestRule
forTestClass(com.google.common.base.Supplier<T> productSupplier)
           
static org.junit.rules.TestRule forTestInContext(Object testInstance)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

forTestClass

public static <T extends TestedProduct<?>> InjectingTestRule forTestClass(Class<T> productClass)

forTestClass

public static <T extends TestedProduct<?>> InjectingTestRule forTestClass(com.google.common.base.Supplier<T> productSupplier)

forTestInContext

public static org.junit.rules.TestRule forTestInContext(Object testInstance)

forTest

public static <T extends TestedProduct<?>> org.junit.rules.TestRule forTest(Object testInstance,
                                                                            Class<T> testedProductClass)

forTest

public static <T extends TestedProduct<?>> org.junit.rules.TestRule forTest(Object testInstance,
                                                                            com.google.common.base.Supplier<T> productSupplier)


Copyright © 2014 Atlassian. All rights reserved.