Hi Ktor lovers :wave: TL;DR - What is the best ap...
# ktor
y
Hi Ktor lovers 👋 TL;DR - What is the best approach for integration test of application that do batch processing with no input interface? Detaisl: I have some really noobish question about tests. I want to write integration test for a ktor service that basically doing some kind of batch processing without any input from the outside world (well, if we exclude timer as input). My idea was to write an integration test that mocks the remote server I'm calling, and use test containers to store the data in the database, so I can actually use the
awaitability
library and wait for the data to be available. With the horrible help of ChatGPT I wrote the following test:
Copy code
@Test
    fun `should fetch comics and store it in the database`() =
        testApplication {
            // Given
            environment {
                withLastComicId()
                withComic(1)
            }

            // When
            application {
                module()
            }

            // Then

            val repository: WebComicsPort by inject()
            val comics =
                await untilNotNull { runBlocking { repository.getComicsById(2940) } }
            comics shouldNotBe null
        }
The problem I'm facing is that actually nothing happens, I see that the application module function is called and then everything stucks. My assumption was that the
awaitability
library using
Thread.sleep()
internally and locking the thread, but even when writing some custom code it still doesn't work 😅 Thanks in advance 🙏
m
I'm not sure that helps, but if you don't call any endpoint here I think you need to manually startApplication() before injecting repository
s
Awaitility handles all logic (and sleeps) behind delays, intervals, repetitions etc on a different thread then the thread executing lambda, so I don't think this could be an issue. Even if the lambda passed to
untilNotNull
would lock the inner thread forever,
awaitility
should still stop throw
ConditionTimeoutException
after configured timeout (defaults to 10 seconds) and kill inner thread performing lambda execution. In your case I would put awaitiliy aside for a moment and check if
repository
is correctly injected, and if
repository.getComicsById
is being correctly invoked and returns proper value
y
@Mariusz Sołtysiak thanks! I'll try that, but I do see that the module is called, just that the part that actually execute the cronjob is never called @Szymon Jeziorski That's some really good point, will try that to debug, thanks!