Hi all, I am following <https://ktor.io/docs/testi...
# ktor
m
Hi all, I am following https://ktor.io/docs/testing.html#hocon guide on testing, the issue I am facing is when I do
withApplication(testEnv)
it doesn't load my module that is defined in
application.conf
but when I do
withTestApplication(Application::main)
it loads the module but the configuration is missing, not sure what am I doing wrong here?
a
Where is your
application.conf
file is located?
m
main/resources
a
It should reside in the resources of the test source set.
m
oh, so I need to have different set of config or I guess I can somehow import
main
config file and change in test what I want?
nop, that is not it, just checked and it loads
application.conf
from main resources without a problem
a
You can use
ConfigFactory.parseFile(File("/path/to/file")
code to load a config from local filesystem.
m
I am using
ConfigFactory.load("application.conf")
just fine, it loads the config file from
main/resources
the issue is that it doesn't start he module specified in that config
a
Could you please try to introduce a syntax error to your config to check that it's actually used?
If the config file doesn't exists in the test source set then there would be no error.
m
it fails when I add a syntax error
a
Could you please share a complete code for your test?
m
Copy code
@Test
    fun `test something`() {
        withApplication(createTestEnvironment { HoconApplicationConfig(ConfigFactory.load("application.conf")) }) {
            with(
                handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "my/endpoint") {
                    addHeader(HttpHeaders.ContentType, ContentType.Application.Json.contentType)
                    setBody("")
                }
            ) {
                assertEquals(HttpStatusCode.OK, response.status())
            }
        }
    }
a
You need to set the
config
property to make it work. So before
HoconApplicationConfig(
add
config =
m
yes! that works, thank you!!