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 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()) |
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
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( |
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
// 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, |
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
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 { |
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
open class User( | |
private val firstName: String, | |
private val lastName: String) { | |
open fun fullName(): String = "$firstName $lastName" | |
} |
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
@RunWith(KotlinTestRunner::class) | |
class GreeterTest { | |
... | |
} |
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
class GreeterTest { | |
var user: User = mock() | |
val tested = Greeter(user) | |
@Test | |
fun englishGreetIsCorrect() { | |
Mockito.`when`(user.fullName()).thenReturn("Fábio Carballo") |
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
fun <String> on(methodCall: LocationProvider.() -> String): OngoingStubbing<String> { | |
return try { | |
Mockito.`when`(mock.methodCall()) | |
} catch(e: NullPointerException) { | |
throw Exception(....) | |
} | |
} |
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
class Coordinate(latitude: Double, longitude: Double) | |
interface LocationProvider { | |
fun getLastKnownLocation(): Coordinate | |
fun getExactLocation(): Coordinate | |
} | |
interface TemperatureProvider { | |
fun getCelsiusTemperatureAt(coordinate: Coordinate): Float |
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
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() |
OlderNewer