Skip to content
Kona

Database & fakes

DatabaseTest wraps Cadence connections for tests.

API Behavior
transactional(database, block) Runs block inside database.connection().transaction(…) and returns the block result
refresh(database, tables) DELETE FROM each listed table name (order matters for FKs)
DatabaseTest.transactional(database) { db ->
// inserts that roll back when the transaction ends without commit
db
}
DatabaseTest.refresh(database, listOf("password_reset_tokens", "users"))

refresh issues raw DELETE FROM $table statements — only pass trusted table identifiers.

Each fake owns an in-memory (or log-backed) collaborator you can assert against. They are plain constructors — bind them into the container in your test provider when the production code resolves the real type.

LogTransport + Mailer. messages() returns captured MailMessages.

val mail = FakeMail()
mail.mailer.send(MailMessage().to("a@b.c").subject("Hi"))
assertEquals(1, mail.messages().size)

Array-driver QueueManager + Bus. dispatch(job) and size() inspect the connection.

val queue = FakeQueue()
queue.dispatch(MyJob())
assertEquals(1L, queue.size())

In-memory DatabaseChannel + LogChannel behind NotificationSender. assertSent(type) is true if either channel recorded that notification class.

val notifications = FakeNotification()
notifications.send(user, WelcomeNotification())
assertTrue(notifications.assertSent(WelcomeNotification::class.java))

Wraps EventDispatcher and records every dispatch argument. assertDispatched(type), events(), clear().

val events = FakeEvent()
events.dispatch(OrderPlaced(id = 1))
assertTrue(events.assertDispatched(OrderPlaced::class.java))
events.clear()

ArrayCacheStore + Repository. Mirrors get / put / forever / forget / flush / has.

val cache = FakeCache()
cache.put("k", "v", 60)
assertEquals("v", cache.get("k"))

Log-driver BroadcastManager. broadcast(event), messages(), clear().

val broadcast = FakeBroadcast()
broadcast.broadcast(OrderShipped())
assertTrue(broadcast.messages().isNotEmpty())

Full in-memory FilesystemAdapter: put/get/exists/delete/copy/move, directories, visibility, streams, putFile / putFileAs. Temporary URLs throw IllegalStateException (providesTemporaryUrls() is false).

val disk = FakeStorage()
disk.put("a.txt", "hello")
assertEquals("hello", disk.getString("a.txt"))

Prefer fakes when the unit under test should not touch Redis, SMTP, or the filesystem. Prefer a real SQLite/H2 Cadence connection (with DatabaseTest.transactional) when you are asserting SQL behavior itself.