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.Before
import org.junit.Test
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import javax.inject.Inject
import javax.servlet.http.HttpServletRequest
interface Database {
fun findUser(): User
}
class User(val name:String)
class MyComponent {
@Inject private lateinit var request: HttpServletRequest
@Inject private lateinit var database: Database
val user: User by lazy {
database.findUser()
}
fun getUsername(): String {
return user.name
}
}
class MyComponentTest {
@Mock private lateinit var request: HttpServletRequest
@Mock private lateinit var database: Database
@InjectMocks private lateinit var sut: MyComponent
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
}
@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.