Unit tests @Transactional - Could not commit JPA transaction; transaction marked as rollback only
<p>I am writing JUnit tests for my Spring app. For one of the scenarios I am getting the exception</p>

<p><code>Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly.</code></p>

<p>I have read some other questions and answers and they did not help. The scenario I am having problems is pretty simple. I want to check that a unique constraint of an entity field is enforced. I create two entities with the same field and expect a constraint violation exception. The calling method is marked as <code>@Transactional</code>, because I want the whole thing to rollback if an exception occurs.</p>

<p>This is the code:</p>

<pre><code>@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DeadlinesApplication.class)
@Rollback(value = false)
public class UserHelperTest {
    @Autowired
    private UserHelper userHelper;

    @Test(expected = DataIntegrityViolationException.class)
    @Transactional
    public void uniqueUsername() throws NullParameterException {
        User user = userHelper.createUser("uniq1", "password", null, null);
        User user2 = userHelper.createUser("uniq2", "password", null, null);
        User user3 = userHelper.createUser("uniq2", "password", null, null);
    }
</code></pre>

<p>UserHelper class is called:</p>

<pre><code>@Service
public class UserHelper {
    @Autowired
    private UserDAO userDAO;


    public User createUser(...) throws NullParameterException {
        User newUser = new User(username, ...);

        userDAO.save(newUser);

        return newUser;
    }
</code></pre>

<p>UserDAO's <code>save()</code> method subsequently calls <code>save()</code> on <code>UserRepository</code>:</p>

<pre><code>@Repository
public interface UserRepository extends CrudRepository&lt;User, Long&gt; {
    public User findByUsername(String username);
}
</code></pre>

<p>The only place with <code>@Transactional</code> is the test method. It needs to be there, because in a separate test method I am lazy loading a many-to-many relation and that needs to happen inside one transaction.</p>

<p>Can you see anything wrong with this code?</p>

<p>EDIT: Error stacktrace</p>

<pre><code>org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly

    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
    at org.springframework.test.context.transaction.TransactionContext.endTransaction(TransactionContext.java:128)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:224)
    at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:313)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:94)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:74)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
    ... 29 more

2016-03-26 20:53:52.913  INFO 2448 --- [       Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@20deea7f: startup date [Sat Mar 26 20:53:44 CET 2016]; root of context hierarchy
2016-03-26 20:53:52.924  INFO 2448 --- [       Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
</code></pre>