Philipp Mayer
06/10/2020, 12:38 PMclass MqttHandler : SensorExchangeUseCase {
/**
* Factory to read the application.conf file
* <https://stackoverflow.com/questions/53891189/how-do-i-use-custom-configuration-in-ktor>
*/
private val config: MqttConfig = with(ConfigFactory.load()) {
MqttConfig(
this.getString("mqtt.topic"),
getString("mqtt.brokerUrl"),
getInt("mqtt.port"),
getString("mqtt.clientId")
)
}
To test that, I created the following test:
class MqttHandlerTest : FunSpec({
withTestApplication {
(environment.config as MapApplicationConfig).apply {
put("mqtt.topic", "karlheinz")
put("mqtt.brokerUrl", container.host)
put("mqtt.port", port.toString())
put("mqtt.clientId", "Karl")
}
main()
}
test("should read correct Config from HOCON File") {
with(ConfigFactory.load()) {
getString("mqtt.topic") shouldBe "karlheinz"
getString("mqtt.brokerUrl") shouldBe container.host
getInt("mqtt.port") shouldBe port
getString("mqtt.clientId") shouldBe "Karl"
}
}
However, my test fails since it loads the values which are in my local file and not the values that I put there with withTestApplication
.
My Service is not an application module. How could I test the behaviour? It feels like I'm missing something.
Thanks in advance!mp
06/10/2020, 3:02 PM