title: Unable to pass beans from testContext to applicationContext using spring and junit
body: <p>I have a test automation library that I can get to work with dagger and guice, but I'm trying to change the project over to using spring-test (per a client's request). </p>
  
  <p>Here's what the test runner originally looked like, with the simpler DI approach.. </p>
  
  <p><strong>JUnit Runner (Pre-Spring)</strong></p>
  
  <pre><code>public class AutomationJUnit4Runner extends ParentRunner&lt;Runner&gt;
  {
    private final Runner runner;
  
    public AutomationJUnit4Runner(Class&lt;?&gt; testClass) throws InitializationError
    {
      super(testClass);
  
      boolean proxied = testClass.isAnnotationPresent(EnableProxiedWebDriver.class);
      this.runner = browserRunners(testClass, ConfigHolder.CAPABILITIES, proxied);
    }
  
    private Runner browserRunners(final Class&lt;?&gt; klass, Iterable&lt;DesiredCapabilities&gt; capabilities, boolean proxiedWebDriver)
            throws InitializationError
    {
      final List&lt;Runner&gt; runners = Lists.newArrayList();
  
      for(DesiredCapabilities c : capabilities)
      {
        runners.add(new BrowserRunner(klass, c, proxiedWebDriver));
      }
  
      return new Suite(klass, runners){ };
    }
  
     private static class BrowserRunner extends BlockJUnit4ClassRunner
     {
        private final DesiredCapabilities capabilities;
        private boolean isProxiedWebDriver;
  
        BrowserRunner(Class&lt;?&gt; testClass, Capabilities capabilities, boolean proxiedWebDriver) throws InitializationError
        {
          super(testClass);
          this.capabilities = new DesiredCapabilities(capabilities);
          this.isProxiedWebDriver = proxiedWebDriver;
        }   
  
        @Override
        protected Object createTest() throws Exception
        {
          Object test = super.createTest();
  
          if(test instanceof RequiresInjection)
          {
            WebDriverModule module = new WebDriverModule(capabilities, isProxiedWebDriver);
            ObjectGraph objectGraph = ObjectGraph.create(concat(getModules((RequiresInjection) test), module));
  
            objectGraph.inject(test);
          }
          return test;
        }
  </code></pre>
  
  <p>I tried switching the tests over to using the Spring junit runner, but I ran into an exception regarding the capabilities:</p>
  
  <p>Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'WebDriverModule': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.openqa.selenium.Capabilities' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}</p>
  
  <p>It makes sense, as the capabilities are loaded at runtime (it's done to support running the suite against multiple browsers), so I figured I would see if there was a way to get those capabilities injected into the context.</p>
  
  <p>The TestExecutionListeners in the Spring documentation looked promising, so I gave it a try.. </p>
  
  <p><strong>JUnit Runner - Spring</strong></p>
  
  <pre><code>  private static class BrowserRunner extends SpringJUnit4ClassRunner
    {
      // .. skipping over misc.
  
      @Override
      protected Object createTest() throws Exception
      {
          TestContextManager testContextManager = getTestContextManager();
          testContextManager.registerTestExecutionListeners(new WebDriverTestExecutionListener(capabilities, isProxiedWebDriver));
  
          return super.createTest();
      }
  </code></pre>
  
  <p><strong>WebDriverTestExecutionListener</strong></p>
  
  <pre><code>@Override
  public void beforeTestClass(TestContext testContext) throws Exception {
  
      ApplicationContext tc = testContext.getApplicationContext();
      if (tc instanceof ConfigurableApplicationContext)
      {
          ConfigurableApplicationContext configurableTestContext = (ConfigurableApplicationContext) tc;
          ConfigurableListableBeanFactory beanFactory = configurableTestContext.getBeanFactory();
          beanFactory.registerResolvableDependency(Capabilities.class, capabilities);
          // todo: figure out isProxied after 'caps' is working
          configurableTestContext.refresh();
      }
  
  }
  </code></pre>
  
  <p>However, I'm still running into the same issue. The "beforeTestClass" isn't called in time to satisfy the dependencies of the page objects.</p>
  
  <p>Is there another way to make these capabilities available to the tests and page objects the moment I instantiate them?</p>