3 Replies Latest reply on Aug 5, 2013 8:55 AM by jrhite_boss

    A use case for @BeforeXXX running in container in Arquillian remote tests? What's the current status?

    jrhite_boss Newbie

      I read in another thread that excecution of BeforeXXX happens outside the container, but may be moving to be executed inside the container (or both)? What's the current status of this?

       

      I have a use case which makes this behavior a bit annoying because I need to set a static variable in my test class.

       

      My use case:

       

      I'm using the persistence extension to populate my db with @UsingDataSet from a json file. Pretty slick so far!

       

      So if I have a test, UserTest, that tests some stuff about my user I will have a file like:

       

      com.mycompany.entity.UserTest.json:

      {

        "users":

        [

          {

            "id": 1,

            "email": "test@example.com",

            "first_name": "test",

            ...,

             ...

          }

        ]

      }

       

      This gets inserted into the DB and so far so good. But I find my test code ends up looking a lot like:

       

      @Inject private UserAccess userAccess;

       

      @Test

      public void getUserByEmail() {

          User user = userAccess.getByEmail("test@example.com");

       

          User expectedUser = new UserBuilder().withEmail(...).withFirstName(...).build();

       

          UserAssert.assertEquals(expectedUser, user);

      }

       

       

      But this is really annoying because it means I have to store my test data both in the test data .json file and also hard-code it into my tests itself. Particularly tedious and error prone if there are lots of fields.

       

      So I figured I'd take advantage of a library like GSON to convert the json file into actual User objects instead of building my own by hand. This way the test data truly lives in only one place. Then all I really need to know is the primary key (or however I choose to store my rows in a map), so in my test class I would have:

       

                private static Map<Class<? extends DatabaseEntity>, Map<Long, ? extends DatabaseEntity>> entitiesMap;

       

                @Inject private UserAccess userAccess;

       

                @BeforeClass

                public static void setUpTestData()  {

            try {

                entitiesMap = JsonEntityConverter.readEntities(UserAccessTest.class);

            } catch (Throwable t) {

                System.err.println(t);

            }

                }

       

        @Test

         public void getUserByEmail() {

            User user = userAccess.getByEmail("test@example.com");

            User expectedUser = (User)(entitiesMap.get(User.class).get(1));      // entitiesMap is currently null because it gets setup in a different JVM :-(


            UserAssert.assertEquals(expectedUser, user);

        }

       

      I can only think of some clumsy ways to solve my issue given that BeforeClass happens outside the container.

       

      Any recommendations on how to solve this given the current code.