Skip to content

Instantly share code, notes, and snippets.

Avatar
🐧
I come from Earth

Tam H. Doan nhoxbypass

🐧
I come from Earth
View GitHub Profile
View MyPresenterTest.kt
class MyPresenterTest {
@Test
fun onOpenCameraButtonClick_postM() {
// Arrange
Mockito.`when`(mockBuildProvider.isMarshmallowAndAbove()).thenReturn(true)
// Action
presenter.onCameraButtonClick()
// Assert
View MyPresenterTest.kt
class MyPresenterTest {
@Mock
private lateinit var mockBuildProvider: BuildVersionProvider
@Mock
private lateinit var mockView: MyView
private lateinit var presenter: MyPresenter
@Before
View MyPresenter.kt
class MyPresenter(val view: MyView, val buildProvider: BuildVersionProvider) : BasePresenter() {
override fun onCameraButtonClick() {
if (buildProvider.isMarshmallowAndAbove()) {
view.requestCameraPermission()
} else {
navigator.openCamera()
}
}
}
View MyPresenterTest.kt
class MyPresenterTest {
@Test
fun onOpenCameraButtonClick_postM() {
setFinalStatic(Build.VERSION::class.java.getField("SDK_INT"), 23)
// Verify flow request Runtime Permissions
// ...
}
@After
View ReflectionHelper.kt
@Throws(Exception::class)
fun setFinalStatic(field: Field, newValue: Any) {
field.setAccessible(true)
val modifiersField = Field::class.java.getDeclaredField("modifiers")
modifiersField.setAccessible(true)
modifiersField.setInt(field, field.getModifiers() and Modifier.FINAL.inv())
field.set(null, newValue)
}
View MyPresenterTest.kt
@RunWith(PowerMockRunner::class)
@PrepareForTest(Build.VERSION::class)
class MyPresenterTest {
@Test
fun onOpenCameraButtonClick_postM() {
Whitebox.setInternalState(Build.VERSION::class.java, "SDK_INT", 23)
// Verify flow request Runtime Permissions
// ...
}
View utils_kotlin_second_approach.kt
@file:JvmName("SaberUtils")
@file:JvmMultifileClass
fun makeLightSaber(powers: Int): LightSaber {
return LightSaber(powers)
}
View utils_kotlin_first_approach.kt
object SaberUtils {
fun makeLightSaber(powers: Int): LightSaber {
return LightSaber(powers)
}
}
View utils_kotlin_jvm_static.kt
object SaberFactory {
@JvmStatic fun makeStaticSaber() { /*..*/ }
fun makeNonStaticSaber() { /*..*/ }
}
View utils_kotlin_package_level_darksaber.kt
// darksaber.kt
@file:JvmName("SaberUtils")
@file:JvmMultifileClass
package org.example
fun makeDarkSaber() { /*...*/ }