Skip to content

Commit a87f58f

Browse files
TransactionTest: assert exception messages.
1 parent 8f80445 commit a87f58f

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,28 +165,28 @@ public void testTransactionReset() {
165165
transaction.abort();
166166
}
167167

168-
@Test(expected = IllegalStateException.class)
168+
@Test
169169
public void testCreateCursorAfterAbortException() {
170170
Transaction tx = store.beginReadTx();
171171
tx.abort();
172-
tx.createKeyValueCursor();
172+
IllegalStateException ex = assertThrows(IllegalStateException.class, tx::createKeyValueCursor);
173+
assertTrue(ex.getMessage().contains("TX is not active anymore"));
173174
}
174175

175-
@Test(expected = IllegalStateException.class)
176+
@Test
176177
public void testCommitAfterAbortException() {
177178
Transaction tx = store.beginTx();
178179
tx.abort();
179-
tx.commit();
180+
IllegalStateException ex = assertThrows(IllegalStateException.class, tx::commit);
181+
assertTrue(ex.getMessage().contains("TX is not active anymore"));
180182
}
181183

182-
@Test(expected = IllegalStateException.class)
184+
@Test
183185
public void testCommitReadTxException() {
184186
Transaction tx = store.beginReadTx();
185-
try {
186-
tx.commit();
187-
} finally {
188-
tx.abort();
189-
}
187+
IllegalStateException ex = assertThrows(IllegalStateException.class, tx::commit);
188+
assertEquals("Read transactions may not be committed - use abort instead", ex.getMessage());
189+
tx.abort();
190190
}
191191

192192
@Test
@@ -195,18 +195,19 @@ public void testCommitReadTxException_exceptionListener() {
195195
DbExceptionListener exceptionListener = e -> exs[0] = e;
196196
Transaction tx = store.beginReadTx();
197197
store.setDbExceptionListener(exceptionListener);
198-
try {
199-
tx.commit();
200-
fail("Should have thrown");
201-
} catch (IllegalStateException e) {
202-
tx.abort();
203-
assertSame(e, exs[0]);
204-
}
198+
IllegalStateException e = assertThrows(IllegalStateException.class, tx::commit);
199+
tx.abort();
200+
assertSame(e, exs[0]);
205201
}
206202

207-
@Test(expected = IllegalStateException.class)
203+
@Test
208204
public void testCancelExceptionOutsideDbExceptionListener() {
209-
DbExceptionListener.cancelCurrentException();
205+
IllegalStateException e = assertThrows(
206+
IllegalStateException.class,
207+
DbExceptionListener::cancelCurrentException
208+
);
209+
assertEquals("Canceling Java exceptions can only be done from inside exception listeners",
210+
e.getMessage());
210211
}
211212

212213
@Test
@@ -388,9 +389,13 @@ public void testRunInReadTx_recursiveWriteTxFails() {
388389
});
389390
}
390391

391-
@Test(expected = DbException.class)
392+
@Test
392393
public void testRunInReadTx_putFails() {
393-
store.runInReadTx(() -> getTestEntityBox().put(new TestEntity()));
394+
DbException e = assertThrows(
395+
DbException.class,
396+
() -> store.runInReadTx(() -> getTestEntityBox().put(new TestEntity()))
397+
);
398+
assertEquals("Cannot put in read transaction", e.getMessage());
394399
}
395400

396401
@Test

0 commit comments

Comments
 (0)