jbnizet
11/17/2019, 11:25 PMJukka Siivonen
11/18/2019, 8:18 AMJukka Siivonen
11/18/2019, 8:21 AMkqr
11/18/2019, 9:09 AMCzar
11/18/2019, 9:10 AMsave()
method, there is no spring to initialize the "static" variable. After encountering this once in the codebase you'll remember to do that. It's only confusing for the first time, which is a shame, but manageable.
In integration tests there should be no mocks, or they should be handled on the @TestConfiguration
level, not injected directly into the services. If you're injecting mocks directly, then this is not an integration test and there should be no Spring context to worry about.Czar
11/18/2019, 9:22 AM*_REPOSITORY
directly, but then it either can't be private or you'll do reflection magic, which is too dirty to consider doing each time you want it mocked.
If you have a generic repository, it could be something like this:
StaticRepositoryInitializerConfigKt.STATIC_REPOSITORY = mockk<EntityRepository>() { /*...*/ }
But I would not recommend that, as someone could define different mock and tests could conflict with each other.
That's why static/global stuff should always be approached carefully.Czar
11/18/2019, 10:07 AMclass UserService {
fun createUser() {
CreateUser(...).execute()
UserCreated(...).publish()
}
for
class UserService(
private val commandService: CommandService,
private val eventBus: EventBust
) {
fun createUser() {
commandService.execute(CreateUser(...))
eventBus.publish(UserCreated(...))
}
nfrankel
11/18/2019, 11:24 AMMike
11/18/2019, 12:04 PMthanksforallthefish
11/18/2019, 12:11 PMMike
11/18/2019, 12:13 PMjbnizet
11/18/2019, 12:54 PMnfrankel
11/18/2019, 12:55 PMjbnizet
11/18/2019, 12:56 PMSay you create a mock repository and inject it into a service that you want to unit-test.
nfrankel
11/18/2019, 12:56 PMnfrankel
11/18/2019, 12:57 PMAnd it makes the code impossible to test