3 Replies Latest reply on Jul 19, 2013 10:35 AM by Bartosz Majsak

    Use multiple databases in one Test-Class with Arquillian

    John Doe Newbie

      Hi everyone!

       

      I have been searching for days now to find an acceptable solution for my needs, but I still can't figure out a way to do it ... I hope someone here can help me cause it's getting more and more frustrating.

      And by the way - i use JBoss 7.1.1 and Arquillian 1.0.3, with the persistence extension 1.0.5CR5

       

      What I need is:

       

      I have a Test- Class, in which i have multiple @Test. And the thing is, that i want this tests to run on different databases, or, run each test on multiple databases. So either tell each test "you run on database1 and you on database2", or let the test run on both databases.

      So after i googled a bit, i convinced myself that it should work when i set up multiple @Deployment in my Test, in each deployment i bind the correct persistence.xml (with the correct DataSource), and then tell each test what to do. But, i only run into errors..

       

      Here's what I did:

       

      I configured multiple container in my arquillian.xml like this:

       

       

      <?xml version="1.0" encoding="UTF-8"?>
      <arquillian xmlns="http://jboss.org/schema/arquillian"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://jboss.org/schema/arquillian
              http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
      
          <!-- Force the use of the Servlet 3.0 protocol with all containers, as it 
              is the most mature -->
          <defaultProtocol type="Servlet 3.0" />
      
          <group qualifier="containerGroup">
              <container qualifier="jboss_1">
                  <configuration>
                      <property name="managementAddress">localhost</property>
                      <property name="managementPort">8080</property>
                  </configuration>
              </container>
      
              <container qualifier="jboss_2" default="true">
                  <configuration>
                      <property name="managementAddress">localhost</property>
                      <property name="managementPort">8080</property>
                  </configuration>
              </container>
          </group>
      </arquillian>
      

       

      So, here is my Test-Class:

       

       

      @RunWith(Arquillian.class)
      public class AccountCalculationTest 
      {
          
          private final static Logger logger = Logger.getLogger( AccountCalculationTest.class.getCanonicalName() );
          
          @EJB
          AccountCalculationDao accountCalculationDao;
          
          
          @Deployment(name = "arch1", order = 1)
          public static Archive<?> createTestArchive()
          {
              final Archive<?> arch = TestUtils.createTestArchive();
              return arch;
          }
          
          @Deployment(name = "arch2", order = 2)
          @TargetsContainer("jboss_2")
          public static Archive<?> createTestArchive2()
          {
              final Archive<?> arch2 = TestUtils.createTestArchive();
              return arch2;
          }
          
          
          
          @Before
          public void setUp()
          {
              try
              {
                  logger.log( Level.INFO, "------------------ @Before ------------------" );
                  logger.log( Level.INFO, "-- execeuting SQL-Scripts (DELETE/INSERT) --" );
                  TestUtils.deleteTestData();
                  TestUtils.insertTestData();
              }
              catch ( final Exception e )
              {
                  assertFalse( "@Before was not executed correctly!" + e.getMessage(), true );
              }
          }
          
          @After
          public void tearDown()
          {
              try
              {
                  logger.log( Level.INFO, "-------------- @After --------------" );
                  logger.log( Level.INFO, "-- execeuting SQL-Script (DELETE) --" );
                  TestUtils.deleteTestData();
              }
      
              catch ( final Exception e )
              {
                  assertFalse( "@After was not execeuted correctly" + e.getMessage(), true );
              }
          }
          
          @Test 
          public void testGetTAZ() throws Exception
          {
              try
              {
                     // do something here
              }
              catch ( final Exception e )
              {
                  assertTrue( "problem while running test method: " + e + " " + e.getLocalizedMessage(), false );
              }
          }
      }
      

       

       

      But, when i run my test - i do this via UI in eclipse, so I go --> "Run as" --> "JUnit Test";

      I get following error:

       

      org.jboss.arquillian.container.test.impl.client.deployment.ValidationException: DeploymentScenario contains a target (jboss_2) not matching any defined Container in the registry.

      Possible causes are: None of the 1 Containers are marked as default or you have defined a @Deployment with a @TargetsContainer of value (jboss_2) that does not match any found/configured Containers (default), see arquillian.xml container@qualifier

          at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.throwNoMatchFound(DeploymentGenerator.java:257)

          at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.throwTargetNotFoundValidationException(DeploymentGenerator.java:245)

          at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.validate(DeploymentGenerator.java:102)

          at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.generateDeployment(DeploymentGenerator.java:84)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

          at java.lang.reflect.Method.invoke(Method.java:597)

          at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)

          at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)

          at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)

          at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)

          at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)

          at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)

          at org.jboss.arquillian.container.test.impl.client.ContainerEventController.execute(ContainerEventController.java:100)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

          at java.lang.reflect.Method.invoke(Method.java:597)

          at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)

          at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)

          at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)

          at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:75)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

          at java.lang.reflect.Method.invoke(Method.java:597)

          at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)

          at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)

          at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:60)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

          at java.lang.reflect.Method.invoke(Method.java:597)

          at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)

          at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)

          at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)

          at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)

          at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.beforeClass(EventTestRunnerAdaptor.java:80)

          at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:182)

          at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:314)

          at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46)

          at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:199)

          at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

          at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:147)

          at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

          at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

       

       

      Why the error is telling me that there is no container jboss_2, when i put it in my xml?

      I hope someone can help me ...

       

      Thanks.

       

      -john