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.