I have a method
fun getUser(userId: UserId): Optional<User?> = userRepository.findById(userId)
that returns an Optional in Java. So I want to mock that method call in my Kotlin unit test.
This was my first guess...
given { mockedUserService.getUser(currentUser.userId) }.willReturn(Optional.of(currentUser))
... but the compiler says no
Type mismatch: inferred type is Optional but Optional<User?>! was expected
So I started to make the val currentUser: User?
just to make the compiler happy.
given { currentUser?.userId?.let { mockedUserService.getUser(it) }.willReturn(Optional.of(currentUser))
Type mismatch: inferred type is Optional but Optional<User?>? was expected
Type mismatch: inferred type is User? but TypeVariable(T) was expected
And now I am somewhat lost. How can I make the compiler happy?
UserService.getUser(userId: UserId)
including the return type. – ChristianB