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

    Static declaration

    roman.valina Newbie


      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.



      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>();



          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)


          public void insertTest() {


              usersInDb = new ArrayList<User>();


              User user1 = new User();






              User user2 = new User();






              for (User user : usersInDb) {






          @InSequence(value = 2)


          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:




          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>();



             public void should_store_currency_code() throws Exception



                assertEquals(1, currencyCodes.size());




             public void should_have_new_list_created() throws Exception








          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!




          • 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)



                private EntityManager entityManager;


            This is my configuration of persistent unit:


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





                        <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" />




            Thanky you for your advices.