Skip to content

Commit 47ba3c2

Browse files
committed
Add test for a throwing constructor (all-args)
1 parent 3f889f0 commit 47ba3c2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

tests/objectbox-java-test/src/main/java/io/objectbox/TestEntity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
/** In "real" entity would be annotated with @Entity. */
2020
public class TestEntity {
2121

22+
public static final String STRING_VALUE_THROW_IN_CONSTRUCTOR =
23+
"Hey constructor, please throw an exception. Thank you!";
24+
25+
public static final String EXCEPTION_IN_CONSTRUCTOR_MESSAGE =
26+
"Hello, this is an exception from TestEntity constructor";
27+
2228
/** In "real" entity would be annotated with @Id. */
2329
private long id;
2430
private boolean simpleBoolean;
@@ -63,6 +69,9 @@ public TestEntity(long id, boolean simpleBoolean, byte simpleByte, short simpleS
6369
this.simpleShortU = simpleShortU;
6470
this.simpleIntU = simpleIntU;
6571
this.simpleLongU = simpleLongU;
72+
if (STRING_VALUE_THROW_IN_CONSTRUCTOR.equals(simpleString)) {
73+
throw new RuntimeException(EXCEPTION_IN_CONSTRUCTOR_MESSAGE);
74+
}
6675
}
6776

6877
public long getId() {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,23 @@ public void testRenew() {
261261
transaction.close();
262262
}
263263

264+
@Test
265+
public void testThrowInEntityConstructor() {
266+
insertTestEntities(TestEntity.STRING_VALUE_THROW_IN_CONSTRUCTOR);
267+
268+
Transaction transaction = store.beginReadTx();
269+
Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class);
270+
try {
271+
cursor.get(1);
272+
fail("Should have thrown");
273+
} catch (RuntimeException e) {
274+
assertEquals(TestEntity.EXCEPTION_IN_CONSTRUCTOR_MESSAGE, e.getMessage());
275+
}
276+
277+
cursor.close();
278+
transaction.close();
279+
}
280+
264281
private TestEntity putEntity(Cursor<TestEntity> cursor, String text, int number) {
265282
TestEntity entity = new TestEntity();
266283
entity.setSimpleString(text);

0 commit comments

Comments
 (0)