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