0

initially i am trying to persist data using EntityManager into an H2 in-memory database. persisting is succesfull. but later when im trying to fetch data in to 3 different test cases, first test case is succesfull in fetching, and second and third test cases is throwing null pointer exception. Here im giving skeleton of my code, it would help you in understanding.

    Class Testclass
     {
     @PersistenContext
     EntityManager em;

     @Transactional
     @Before
     public void beforeEachTest
     {
     ClassName obj=new ClassName();
         obj.setName("name");
           em.persist(obj);
      }

     @Test
     @Transactional
      public void testMethod1()
          {
    //fetching values 
       obj.getName();
            }

       @Test
        @Transactional
        public void testMethod2()
        {
         obj.getName();
         }

       @Test
        @Transactional
        public void testMethod3()
        {
         obj.getName();
         }
      }

In the above skeleton code, in method 'beforeEachTest' im trying to persist data. it is succesfull. then im trying to fetch data in all the 3 test methods. but first one is succesfull , 2nd and 3rd test methods were failed and returns nullPointerException. Please give me a solution to come out of this.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
user1379308
  • 31
  • 1
  • 5
  • 2
    Show you real code or at least show how you fetching your entity. – Aleksandr M Nov 02 '12 at 11:49
  • Please also post the NullPointerException, and write at which line of your code it occurs. – Thomas Mueller Nov 02 '12 at 14:03
  • Be aware the in-memory database is discarded, by default, when the connection closes. You can override this behavior. See: [*Keep H2 in-memory database between connections*](https://dba.stackexchange.com/q/224338/19079) and [*H2 in-memory database. Table not found*](https://stackoverflow.com/q/5763747/642706) – Basil Bourque Dec 06 '18 at 21:33

0 Answers0