Skip to content

Commit b139e6a

Browse files
Transaction: test it throws after close (#818)
1 parent 19821f9 commit b139e6a

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/TransactionTest.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.objectbox.exception.DbMaxReadersExceededException;
2222
import org.junit.Ignore;
2323
import org.junit.Test;
24+
import org.junit.function.ThrowingRunnable;
2425

2526
import java.util.ArrayList;
2627
import java.util.concurrent.Callable;
@@ -39,6 +40,7 @@
3940
import static org.junit.Assert.assertNotNull;
4041
import static org.junit.Assert.assertNotSame;
4142
import static org.junit.Assert.assertSame;
43+
import static org.junit.Assert.assertThrows;
4244
import static org.junit.Assert.assertTrue;
4345
import static org.junit.Assert.fail;
4446

@@ -293,12 +295,22 @@ public void testClose() {
293295
// Double close should be fine
294296
tx.close();
295297

296-
try {
297-
tx.reset();
298-
fail("Should have thrown");
299-
} catch (IllegalStateException e) {
300-
// OK
301-
}
298+
// Calling other methods should throw.
299+
assertThrowsTxClosed(tx::commit);
300+
assertThrowsTxClosed(tx::commitAndClose);
301+
assertThrowsTxClosed(tx::abort);
302+
assertThrowsTxClosed(tx::reset);
303+
assertThrowsTxClosed(tx::recycle);
304+
assertThrowsTxClosed(tx::renew);
305+
assertThrowsTxClosed(tx::createKeyValueCursor);
306+
assertThrowsTxClosed(() -> tx.createCursor(TestEntity.class));
307+
assertThrowsTxClosed(tx::isActive);
308+
assertThrowsTxClosed(tx::isRecycled);
309+
}
310+
311+
private void assertThrowsTxClosed(ThrowingRunnable runnable) {
312+
IllegalStateException ex = assertThrows(IllegalStateException.class, runnable);
313+
assertEquals("Transaction is closed", ex.getMessage());
302314
}
303315

304316
@Test

0 commit comments

Comments
 (0)