mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user