Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:58:23+00:00 2026-06-04T18:58:23+00:00

I have a life-cycle problem doing some test suite with JUnit. In order to

  • 0

I have a life-cycle problem doing some test suite with JUnit.

In order to write handy JPA 2.0 unit tests
As a Java developer
I want to:

  • Initialize an EntityManagerFactory instance once before all tests suites. I achieve the object by using the @BeforeClass annotation
  • Instantiate an EntityManager instance and start a new transaction before each test case and rollback the started transaction like AOP before/after or around advice
  • Be able to make any set up/tear down operations before/after in any derived test suite

I’ve been writing JUnit tests a lot. But in such a case I’ve got problem with the second and third items from the list.

Please take a look of the following test suite examples:

An abstract Test suite:

public abstract class AbstractPersistenceTest {

    protected static EntityManagerFactory emf;
    protected EntityManager em;

    @BeforeClass
    public static void setUpClass() {
        emf = Persistence.createEntityManagerFactory("test");
    }

    @Before
    public void setUp() {
        em = emf.createEntityManager();
        em.getTransaction().begin();
    }

    @After
    public void tearDown() {
        em.getTransaction().rollback();
        em.close();
    }

    @AfterClass
    public static void tearDownClass() {
        emf.close();
    }

}

A derived test suite:

public class EmployeeJpqlTest extends AbstractPersistenceTest {

    private Employee john;
    private Employee jack;

    @Before
    public void setUp() {
        john = new Employee("John Doe", 1000);
        jack = new Employee("Jack Line", 1010);

        em.persist(john);
        em.persist(jack);
    }

    @Test
    public void itShouldRetrieveAllEmplloyees() {
        TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e",
                Employee.class);
        List<Employee> employees = query.getResultList();

        assertArrayEquals(new Employee[] { john, jack }, employees.toArray());
    }

    @Test
    public void itShoulRetrieveAllEmployeeNames() {
        TypedQuery<String> query = em.createQuery(
                "SELECT e.name FROM Employee e", String.class);
        List<String> names = query.getResultList();

        assertArrayEquals(new String[] { john.getName(), jack.getName() },
                names.toArray());
    }

}

Because of unspecified order of JUnit life-cycle annotations the NullPointerException take a place in the setUp() method in the derived class. It’s clear for me.

Is it possible to get the goal without injecting starting/rollbacking transaction code in each setUp()/tearDown() methods of any derived test suite class by hand?
Or, maybe, is there an alternative JUnit mean or test framework that can provide an easy way to express my needs?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:58:24+00:00Added an answer on June 4, 2026 at 6:58 pm

    How do you like the idea of using Google Guice to inject the Entity Manager and Transactions into your testing methods?

    import com.google.inject.persist.Transactional;
    import javax.persistence.EntityManager; 
    
    public class MyTest {
            @Inject EntityManager em; 
    
            @Test
            @Transactional 
            public void createNewPerson() {
                    em.persist(new Person(...)); 
            } 
    }
    

    It could simplify a lot of effort in this area.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some issues with my Robotium tests. I have 3 test classes
Being frustrated about fragment behavior, I started doing some testing. I have one activity
Hello i have new Problem with Add Form my problem here WARNING: /test.xhtml @24,173
Is parcelable data persistent? I have some variables that I destroy in android lifecycle
So for example we have real life photo. how to get (relativly to image
I have this code I have written to make my life easier when downloading
I have to make a mobile app that calculates the real life size of
I have a small RIA that I built as a learning/make-my-life-easier project that uses
Most of my development life, I have done a JavaScript generated button like so:
I have a very simple code, which annoyingly was working and for the life

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.