Skip to content

Instantly share code, notes, and snippets.

@miensol
Last active April 5, 2016 19:06
Show Gist options
  • Save miensol/123ade8ba6a4af307cf628a88606c753 to your computer and use it in GitHub Desktop.
Save miensol/123ade8ba6a4af307cf628a88606c753 to your computer and use it in GitHub Desktop.
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