2 Replies Latest reply on Jan 9, 2013 5:31 AM by roman.valina

    Static declaration

    roman.valina Newbie

      Hi

      I am wery new in arquilian but i starting to be a big fun.

      Can anyoboby advice me why this code throws nullPinterException at line User u = usersInDb.get(0); But if I declare the "private List<User> usersInDb = new ArrayList<User>();" static it run s fine?

       

      Thank you.

       

      @RunWith(Arquillian.class)

      public class UserDaoTest {

       

          private static final String APP_NAME = "test";

       

          @EJB(mappedName = "java:global/test/UserDaoImpl")

          private UserDao userDao;

       

          private List<User> usersInDb = new ArrayList<User>();

       

          @Deployment

          public static JavaArchive deploy() throws Exception {

              final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, APP_NAME + ".jar").addClass(UserDaoImpl.class);

              archive.addAsResource("test-persistence.xml", "META-INF/persistence.xml");

              return archive;

          }

       

         

       

          @InSequence(value = 1)

          @Test

          public void insertTest() {

       

              usersInDb = new ArrayList<User>();

       

              User user1 = new User();

              user1.setId(1l);

              user1.setName("roman");

              user1.setSalary(1);

              usersInDb.add(user1);

       

              User user2 = new User();

              user2.setId(2l);

              user2.setName("karel");

              user2.setSalary(5);

              usersInDb.add(user2);

       

              for (User user : usersInDb) {

                  userDao.insert(user);

              }

       

          }

       

          @InSequence(value = 2)

          @Test

          public void findUserByIdTest() {

              User u = usersInDb.get(0);

              User selectedUser = userDao.findUserById(u.getId());

              assertEquals("roman", selectedUser.getName());

          }

       

      }

        • 1. Re: Static declaration
          Bartosz Majsak Master

          This the way how JUnit works. Each test is invoked on the new instance of test class, therefore field is every time initialized as empty ArrayList. And when it's static... well you know for sure

           

          Here's plain JUnit example to prove it:

           

           

          {code:java}

          import static org.junit.Assert.*;

           

          import java.util.ArrayList;

          import java.util.List;

           

          import org.junit.Test;

           

          public class CurrencyCodeTest

          {

           

              private List<String> currencyCodes = new ArrayList<String>();

           

             @Test

             public void should_store_currency_code() throws Exception

             {

                currencyCodes.add("USD");

                assertEquals(1, currencyCodes.size());

             }

           

              @Test

             public void should_have_new_list_created() throws Exception

             {

                assertTrue(currencyCodes.isEmpty());

             }

           

          }

          {code}

           

          If you want to seed your database for tests have a look at persistence extension or have a look at this guide which shows a bit more pragmatic (not that flexible though) approach.

           

          On the other hand I don't really see any value in this list. You already know what should be in the database so why store it in the list?

           

          Hope that helps!

           

          Cheers,

          Bartosz

          • 2. Re: Static declaration
            roman.valina Newbie

            Oh yes I forgot this basic JUnit feature. The reason why I am want to store users in list is that I have the generated IDs in my user entity and I never know what id will be generated for the entity. I wants to have the test simple when I was asking the question.

            I was already triyng to implement the persistence extension tutorial but the @PersistentContext is not injected from some reason.

             

            Why the EntityManager is not injected when I add following to my test class. (Persisten unit is injected in to the UserDao class with no problem)

             

            @PersistenceContext(unitName="test-persistent-unit")

                private EntityManager entityManager;

             

            This is my configuration of persistent unit:

             

            <persistence-unit name="test-persistent-unit"

                    transaction-type="JTA">

                    <provider>org.hibernate.ejb.HibernatePersistence</provider>

                    <class>com.aspectworks.training.domain.User</class>

                    <properties>

                        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/TrainingDS" />

                        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />

                        <property name="hibernate.connection.password" value="password" />

                        <property name="hibernate.connection.username" value="root" />

                        <property name="hibernate.hbm2ddl.auto" value="create-drop" />

                    </properties>

                </persistence-unit>

             

            Thanky you for your advices.

             

            Roman