Last active
April 5, 2016 19:06
-
-
Save miensol/123ade8ba6a4af307cf628a88606c753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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