Created
January 18, 2021 16:23
-
-
Save kibotu/82bd88c027f52d96d1080e8b3fe7350e to your computer and use it in GitHub Desktop.
static mocking
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
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