TransactionRequiredException on testDB setup code

109 views
Skip to first unread message

Simon Lenz

unread,
Jul 28, 2017, 4:05:25 AM7/28/17
to ninja-framework
Hello all,

I'm trying to do some integration testing on my small application.

Currently I want to do some "unit"-tests with a test database in the background.

I'm following this tutorial, but I don't like the idea of a setup method in my production code. Because of that I want to add my DB setup/deletion code to the @Before method of the unit test.

But somehow I receive a "javax.persistence.TransactionRequiredException: Executing an update/delete query" error on executing my delete query.

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.junit.Before;
import org.junit.Test;

import com.google.inject.persist.Transactional;

import ninja.NinjaTest;

public class PlayerApiControllerIntegrationTest extends NinjaTest {

   
   @Before
   @Transactional
   public void setup() {
      EntityManager entityManager = getInjector().getProvider(EntityManager.class).get();
      Query query = entityManager.createQuery("delete FROM Player");
      query.executeUpdate();
//add test data to fresh DB
   }
   
   @Test
   public void test1() {
      System.out.println("test1");
   }
}

This is my testclass and the error received:

javax.persistence.TransactionRequiredException: Executing an update/delete query

    at org
.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
    at kicko
.controllers.PlayerApiControllerIntegrationTest.setup(PlayerApiControllerIntegrationTest.java:21)

Does anybody have an idea what goes wrong here?

kind regards,
Simon

Patrick

unread,
May 22, 2018, 10:55:13 AM5/22/18
to ninja-framework
Hi,
I have the same problem.
Did you find a solution?
Reply all
Reply to author
Forward
0 new messages