K
K
Kentaro232018-04-26 14:33:49
Java
Kentaro23, 2018-04-26 14:33:49

How to properly lock Persistence.createEntityManagerFactory?

good afternoon! There is a piece of function where I use EntityManagerFactory and EntityManager

em = emf.createEntityManager();

            em.getTransaction().begin();
            em.persist(jt);
            em.getTransaction().commit();
            em.close();
How to correctly lock these entities I tried according to the manual, but it doesn’t work, I fall with an error because the EntityManager null is below the test code
@RunWith(PowerMockRunner.class)
@PrepareForTest(Persistence.class)
@PowerMockIgnore({"javax.persistence.*", "org.mockito.*", "com.package.IDataMapper", "com.package.ICache"})
public class DbLoggerTest {

    @Mock
    private static EntityManager entityManager;

    @Mock
    private static EntityManagerFactory factory;

    private DbLogger logger;

    @Before
    public void init() {

        PowerMockito.mockStatic(Persistence.class);
        factory = mock(EntityManagerFactory.class);
        BDDMockito.given(factory.createEntityManager()).willReturn(entityManager);
        BDDMockito.given(Persistence.createEntityManagerFactory(anyString())).willReturn(factory);

        logger = new DbLogger();
    }

    @Test
    public void log() {
        logger.log(Level.FINE, "FINE");
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question