Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import org.junit.Test
import org.mockito.Mockito
import javax.inject.Inject
import javax.servlet.http.HttpServletRequest
interface Database {
fun findUser(): User
}
class User(val name: String)
inline fun <reified T : Any> mock() = Mockito.mock(T::class.java)
class MyComponent @Inject constructor(val request: HttpServletRequest, val database: Database) {
val user: User by lazy {
database.findUser()
}
fun getUsername(): String {
return user.name
}
}
class MyComponentTest {
val request = mock<HttpServletRequest>()
val database = mock<Database>()
val sut = MyComponent(request, database)
@Test
fun `username`() {
Mockito.`when`(database.findUser()).thenReturn(User("test"))
val username = sut.getUsername()
MatcherAssert.assertThat(username, Matchers.equalTo("test"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.