Christoph Beylage
03/22/2022, 4:56 PMdata class AppConfig(
var smtp = SmtpConfig(
enabled = false
host = "test",
port = 12345,
useStartTLS = true,
fromName = "test",
from = "<mailto:test@test.de|test@test.de>"
user = ""
password = ""
)
)
val classInstance = AppConfig()
what I want to do in one of our tests is to mock the value of smtp.
What I did - here for example with mockito - was the following:
val spy = Mockito.spy(classInstance)
Mockito.`when`(spy.smtp).thenReturn(SmtpConfig(true)). //true is the enabled flag in this case, so the rest should be null
What Mockito and Mockk are doing in this case is to mock the get method behind that property. This can be verified by starting the debugger right in the line after the Mockito.\`when\` expression.
The field is exactly like initialized above and when I click the getter in the debugger I get the result from spy.smtp, as you can see in the screenshot.
I can understand why this happens, but kotlin simply does not use the getters of those properties why the tests wont work.
So long text, simple question: Any idea how to fix this? Everything I found was a github issue from mockk with exactly the same problem and that has been closed because to support this a major rebuild would be necessary.Jordan Stewart
03/22/2022, 5:48 PMAppConfig
instead? e.g. if you have an SmtpConfig
in a variable called x
then you can just AppConfig(x)
and πͺJordan Stewart
03/22/2022, 5:49 PMvar smtp
-> val smtp
-- an app's config changing while it's running might lead you to some tricky to debug problems πChristoph Beylage
03/22/2022, 6:03 PMdata class AppConfig(
var smtp = SmtpConfig(
enabled = getValueFromConfigFile()
host = getValueFromConfigFile(),
port = getValueFromConfigFile(),
useStartTLS = getValueFromConfigFile(),
fromName = getValueFromConfigFile(),
from = getValueFromConfigFile()
user = getValueFromConfigFile()
password = getValueFromConfigFile()
)
)
Christoph Beylage
03/22/2022, 6:05 PMChristoph Beylage
03/22/2022, 6:07 PMJordan Stewart
03/22/2022, 9:14 PMJordan Stewart
03/22/2022, 9:16 PMChristoph Beylage
03/23/2022, 7:37 AMChristoph Beylage
03/23/2022, 7:37 AMJordan Stewart
03/23/2022, 11:15 AMand what we want is a simple AppConfig with useful default values and override some of them in test classes.if you don't want to put "useful default values" into the constructor directly, could you instead have a test config file that contains them, and then use
.copy(..)
to override them?Christoph Beylage
03/23/2022, 11:16 AMJordan Stewart
03/23/2022, 11:17 AMChristoph Beylage
03/23/2022, 11:18 AMJordan Stewart
03/23/2022, 11:20 AMsmtp
with a mock, but not with a real version you hydrate from configuration or just new
up?Jordan Stewart
03/23/2022, 11:20 AMJordan Stewart
03/23/2022, 11:21 AMChristoph Beylage
03/23/2022, 11:22 AMJordan Stewart
03/23/2022, 11:22 AMChristoph Beylage
03/23/2022, 11:23 AMJordan Stewart
03/23/2022, 11:25 AMval
effectively be a var
, but just in the production code it's not? πChristoph Beylage
03/23/2022, 11:27 AMJordan Stewart
03/23/2022, 11:27 AMChristoph Beylage
03/23/2022, 11:29 AMJordan Stewart
03/23/2022, 11:31 AMChristoph Beylage
03/23/2022, 11:31 AMJordan Stewart
03/23/2022, 11:31 AMJordan Stewart
03/23/2022, 11:32 AMJordan Stewart
03/23/2022, 11:32 AMChristoph Beylage
03/23/2022, 11:34 AMJordan Stewart
03/23/2022, 11:38 AMChristoph Beylage
03/23/2022, 11:42 AMJordan Stewart
03/23/2022, 11:44 AMJordan Stewart
03/23/2022, 11:45 AMJordan Stewart
03/23/2022, 11:45 AMChristoph Beylage
03/23/2022, 12:06 PM