Skip to content

Instantly share code, notes, and snippets.

@fbcbl
fbcbl / crash_handling.kt
Last active May 13, 2019 13:31
Crash Handling Part 1
private fun setupCrashHandler() {
// 1. Get the system handler.
val systemHandler = Thread.getDefaultUncaughtExceptionHandler()
// 2. Set the default handler as a dummy (so that crashlytics fallbacks to this one, once set)
Thread.setDefaultUncaughtExceptionHandler { t, e -> /* do nothing */ }
// 3. Setup crashlytics so that it becomes the default handler (and fallbacking to our dummy handler)
Fabric.with(this, Crashlytics())
@fbcbl
fbcbl / crash_handling2.kt
Last active April 26, 2020 15:10
Crash Handling II
class AppExceptionHandler(val systemHandler: Thread.UncaughtExceptionHandler,
val crashlyticsHandler: Thread.UncaughtExceptionHandler,
application: Application) : Thread.UncaughtExceptionHandler {
private var lastStartedActivity: Activity? = null
private var startCount = 0
init {
application.registerActivityLifecycleCallbacks(
@fbcbl
fbcbl / kotlin_testing_pt1_classes.kt
Last active September 29, 2017 22:17
Kotlin Testing Pt.1 Classes
// Class that is going to be the focus of our test
class Greeter(
private val user: User) {
fun getEnglishGreeting() = "Hello, ${user.fullName()}!"
}
// Class used as a dependency
class User(
private val firstName: String,
@fbcbl
fbcbl / kotlin_testing_pt1_test_1.kt
Last active September 29, 2017 22:18
Kotlin Testing Part 1 Test
import org.junit.Before
import org.junit.Test
import org.junit.Assert.*
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
class GreeterTest {
@fbcbl
fbcbl / kotlin_testing_pt1_classes_open.kt
Created September 25, 2017 09:50
Kotlin Testing Pt 1 Classes Open
open class User(
private val firstName: String,
private val lastName: String) {
open fun fullName(): String = "$firstName $lastName"
}
@fbcbl
fbcbl / kotlin_runner_test.kt
Created September 25, 2017 09:59
Kotlin Runner Test
@RunWith(KotlinTestRunner::class)
class GreeterTest {
...
}
@fbcbl
fbcbl / kotlin_testing_pt2_improvement_1.kt
Last active October 8, 2017 18:33
kotlin_testing_pt2_improvement_1
class GreeterTest {
var user: User = mock()
val tested = Greeter(user)
@Test
fun englishGreetIsCorrect() {
Mockito.`when`(user.fullName()).thenReturn("Fábio Carballo")
@fbcbl
fbcbl / kotlin_testing_pt2_on_method.kt
Last active October 9, 2017 10:35
kotlin_testing_pt2_on_method
fun <String> on(methodCall: LocationProvider.() -> String): OngoingStubbing<String> {
return try {
Mockito.`when`(mock.methodCall())
} catch(e: NullPointerException) {
throw Exception(....)
}
}
@fbcbl
fbcbl / kotlin_testing_pt2_example_2.kt
Last active October 9, 2017 12:24
kotlin_testing_pt2_example_2
class Coordinate(latitude: Double, longitude: Double)
interface LocationProvider {
fun getLastKnownLocation(): Coordinate
fun getExactLocation(): Coordinate
}
interface TemperatureProvider {
fun getCelsiusTemperatureAt(coordinate: Coordinate): Float
package testing.fabiocarballo.com.myapplication
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Test
class TemperaturePresenterTest {
val locationProvider: LocationProvider = mock()