Skip to content

Instantly share code, notes, and snippets.

@kibotu
Created January 18, 2021 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kibotu/82bd88c027f52d96d1080e8b3fe7350e to your computer and use it in GitHub Desktop.
Save kibotu/82bd88c027f52d96d1080e8b3fe7350e to your computer and use it in GitHub Desktop.
static mocking
private object Buddy {
@JvmStatic
fun name(): String = "John"
@JvmStatic
fun hello(name: String) = name
}
class StaticTests {
@Test
fun lookMomICanMockStaticMethods() {
assertThat(Buddy.name()).isEqualTo("John")
mockStatic(Buddy::class.java).use {
it.`when`<Any>(Buddy::name).thenReturn("Rafael")
assertThat(Buddy.name()).isEqualTo("Rafael")
}
assertThat(Buddy.name()).isEqualTo("John")
}
@Test
fun returnUtilTest() {
assertEquals("foo", Buddy.hello("foo"))
mockStatic(Buddy::class.java).use {
it.`when`<Any> { Buddy.hello("foo") }.thenReturn("bar")
assertEquals("bar", Buddy.hello("foo"))
}
assertEquals("foo", Buddy.hello("foo"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment