title: Using different java based spring context configurations with cucumber
body: <p>I am trying to get Cucumber working with Spring. In our code, we are already using java based Spring configuration. I am having trouble getting it to work in the following scenario. Can someone please help?</p>
  
  <p>Today , in our integration test classes we use @ContextConfiguration for each class and provide the config class that is declared with in that integration test class for loading the beans. Config class is annotated with @Configuration. Same bean could be instantiated differently in 2 different classes Config classes used in 2 different integration test classes.</p>
  
  <p>So when I use Cucumber, since the Contextconfiguration differs on different classes, it looks for 'Cucumber.xml' . In the xml file, I am using component-scan to scan the cucumber step definition classes by giving the package name that these classes use (both classes have same package name) . Since all beans gets loaded in same context, Cucumber is failing to load the beans when it finds the same bean defined in these different config classes .</p>
  
  <p>How do I get over this problem of creating same bean but in different ways and use them in different classes?<br>
  Please note that I am not looking for a solution that creates lot of churn from our existing coding practices, so having per-test-xml file is not an option for me.</p>
  
  <p>Here is how our code looks:</p>
  
  <p><strong>Class NameAndAddressProviderIntegrationTestSteps :-</strong></p>
  
  <pre><code>@ContextConfiguration(locations="classpath:cucumber.xml")
  public class NameAndAddressProviderIntegrationTestSteps {
  @Configuration
  @Import({
      xyz.class,
      abc.class,
      NameAndAddressProvider.class
  })
  @ImportResource({
          "file:configuration/spring-configuration/abc.xml",
          "file:configuration/spring-configuration/xyz.xml"
  })
  public static class Config {
  
      @Bean
      AccountHolderDataMap dataMap() {
          AccountHolderDataMap data = new AccountHolderDataMap();
          data.put(ID,
                  new AccountHolderData(customerID));
         data.get(customerID).setCustomerplaceID(testCustomerplaceID);
          return data;
      }
  
   }
  @Inject
  private NameAndAddressProvider provider;
  
  @When("^I call nameandAddress provider with a 'customerId'$")
  public void i_call_nameandAddress_provider_with_a_customerId() throws DependencyException {
      System.out.println("Entering when method");
      names = provider.getNames(customerID);
      System.out.println(provider.toString());       
  }
  ......
  }
  </code></pre>
  
  <p><strong>Class AddressProviderIntegrationTestSteps:-</strong></p>
  
  <pre><code> @ContextConfiguration(locations="classpath:cucumber.xml")
   public class AddressProviderIntegrationTestSteps {
   @Configuration
   @Import({
          abc.class,
          xyz.class,
          AddressesProvider.class
  })
  @ImportResource({
          "file:configuration/spring-configuration/test-environment.xml",
          "file:configuration/spring-configuration/test-logging-config.xml"
  })
  public static class Config {
      @Bean
      @DependsOn("Environment")
      AccountHolderDataMap data() {
          AccountHolderDataMap data = new AccountHolderDataMap();
         data.put(testCustomerID,
                  new AccountHolderData(testCustomerID, testCustomerplaceID,businessType));
          return data;
      }
  }
  
  private static final String testCustomerID = "1234";
  private static final String testMarketplaceID = "abc";
  
  @Inject
  private AddressesProvider provider;
  
  @When("^I call AddressesProvider provider with a 'CustomerID'$")
  public void i_call_AddressesProvider_provider_with_a_CustomerID() throws Throwable {
      List&lt;Address&gt; addresses = provider.getAddresses(testCustomerID);
      Log.info(addresses.get(0).toString());
      assertTrue(addresses.size()==1);
    }    
  </code></pre>
  
  <p>}</p>
  
  <p>And here is the nested exception I am getting:-
  "nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [....AccountHolderDataMap] is defined: expected single matching bean but found 2: dataMap,data"</p>
  
  <p>Appreciate your help!</p>