Uploaded image for project: 'Arquillian'
  1. Arquillian
  2. ARQ-2182

@ApplyScriptBefore on test method is executed before methods annotated with @Before

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • persistence_1.0.0.Alpha7
    • None

      I want to use an setUp-method annotated with @Before to apply a script with general preparations, executed before each method of my test class. In this script ("prepare_test.sql") I empty a table and insert some records. For this I used the @ApplyScriptBefore.

      In one of my test cases I want to insert more records and apply another script ("moreusers", which only has some insert statements).

      See this class

      DataSourceTest.java
      @RunWith(Arquillian.class)
      public class DataSourceTest {
      
        @Before
        @ApplyScriptBefore({ "prepare_test.sql" })
        public void setUp() {
          System.out.println("SetUp");
        }
      
        @ApplyScriptBefore({ "moreusers.sql" })
        @Test
        public void GetAllEmps() {
          // The "prepare_test.sql" and the "moreusers.sql" should have been applied
        }
        
        @Test
        public void OtherTest() {
          // Only the "prepare_test.sql" should have been applied
        }  
      }
      
      prepare_test.sql
      delete from bish.EMP;
      
      -- 14 Default-Employes
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7839','KING','PRESIDENT',null,to_date('17.11.81','DD.MM.RR'),'5000',null,'10');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7698','BLAKE','MANAGER','7839',to_date('01.05.81','DD.MM.RR'),'2850',null,'30');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7782','CLARK','MANAGER','7839',to_date('09.06.81','DD.MM.RR'),'2450',null,'10');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7566','JONES','MANAGER','7839',to_date('02.04.81','DD.MM.RR'),'2975',null,'20');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7788','SCOTT','ANALYST','7566',to_date('09.12.82','DD.MM.RR'),'3000',null,'20');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7902','FORD','ANALYST','7566',to_date('03.12.81','DD.MM.RR'),'3000',null,'20');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7369','SMITH','CLERK','7902',to_date('17.12.80','DD.MM.RR'),'800',null,'20');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7499','ALLEN','SALESMAN','7698',to_date('20.02.81','DD.MM.RR'),'1600','300','30');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7521','WARD','SALESMAN','7698',to_date('22.02.81','DD.MM.RR'),'1250','500','30');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7654','MARTIN','SALESMAN','7698',to_date('28.09.81','DD.MM.RR'),'1250','1400','30');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7844','TURNER','SALESMAN','7698',to_date('08.09.81','DD.MM.RR'),'1500','0','30');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7876','ADAMS','CLERK','7788',to_date('12.01.83','DD.MM.RR'),'1100',null,'20');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7900','JAMES','CLERK','7698',to_date('03.12.81','DD.MM.RR'),'950',null,'30');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('7934','MILLER','CLERK','7782',to_date('23.01.82','DD.MM.RR'),'1300',null,'10');
      
      -- Standard noch 2 dazu, macht 16
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('9999','Test','Eins','7782',to_date('23.01.82','DD.MM.RR'),'1300',null,'10');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('9998','Test','Zwei','7782',to_date('23.01.82','DD.MM.RR'),'1300',null,'10');
      
      
      commit;
      
      moreusers.sql
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('9997','Test','Drei','7782',to_date('23.01.82','DD.MM.RR'),'1300',null,'10');
      Insert into bish.EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values ('9996','Test','Vier','7782',to_date('23.01.82','DD.MM.RR'),'1300',null,'10');
      
      
      commit;
      

      I think you get the situation. I won't focus the "OtherTest()" method no more in the description.

      When I now execute the GetAllEmps() I get constraint problems (parent key not found for coloum mgr), because the script "moreusers.sql" is executed before the "prepare_tests.sql".

      If I delete the setUpMethod and annotate the testmethod GetAllEmps() with both scripts

      @ApplyScriptBefore({ "prepare_test.sql", "moreusers.sql" })
      
      

      then the test passes, because the execution order ist correct and both scripts are applied. But I don't want to annotate every test method with the default script which should run before every test, but only annotate those methods which need further scripts/data.

      So I would expect that the method annotated with @Before and its other annotations are executed before a "normal" test method and its annotations are executed. The docs say so, but the descriped scenario above shows that this is not reality.

              bartosz-1 Bartosz Majsak
              bukama Matthias Bünger (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated: