#1760 #1759 - ENH: Add ForTests.rollbackAll(closure) and ForTests.createRollbackAll()

This commit is contained in:
rob bygrave
2019-07-17 14:44:52 +12:00
parent 0663a97941
commit dcf7b60d37
3 changed files with 216 additions and 0 deletions
@@ -0,0 +1,49 @@
package org.tests;
import io.ebean.DB;
import io.ebean.ForTests;
import io.ebean.annotation.Transactional;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.tests.basic.type.BSimpleWithGen;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class ForTestsBeforeAfterTest {
private ForTests.RollbackAll rollbackAll;
@Before
public void before() {
rollbackAll = ForTests.createRollbackAll();
}
@After
public void after() {
rollbackAll.close();
assertThat(getCount()).isEqualTo(0);
}
@Test
public void createRollbackAll() {
doInsert();
assertThat(getCount()).isEqualTo(1);
}
private int getCount() {
return DB.find(BSimpleWithGen.class)
.where().eq("name", "ForTestsBeforeAfterTest")
.findCount();
}
@Transactional
private void doInsert() {
BSimpleWithGen bean = new BSimpleWithGen("ForTestsBeforeAfterTest");
DB.save(bean);
}
}
+54
View File
@@ -0,0 +1,54 @@
package org.tests;
import io.ebean.DB;
import io.ebean.ForTests;
import io.ebean.Transaction;
import io.ebean.annotation.Transactional;
import org.junit.Test;
import org.tests.basic.type.BSimpleWithGen;
import static org.assertj.core.api.Assertions.assertThat;
public class ForTestsTest {
@Test
public void noTransactional_expect_noTransactionEnterExitCalled() {
ForTests.noTransactional(this::checkForTransactional);
}
@Test
public void enableTransactional() {
ForTests.enableTransactional(false);
checkForTransactional();
ForTests.enableTransactional(true);
}
@Transactional
private void checkForTransactional() {
final Transaction transaction = DB.currentTransaction();
assertThat(transaction).isNull();
}
@Test
public void rollbackTransactions() {
DB.find(BSimpleWithGen.class).delete();
ForTests.rollbackAll(this::doInsert);
final int count = DB.find(BSimpleWithGen.class).findCount();
assertThat(count).isEqualTo(0);
}
@Transactional
private void doInsert() {
BSimpleWithGen bean = new BSimpleWithGen("doInsert");
DB.save(bean);
}
}