Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
The Importance of Being Tested
Search
Roberto Orgiu
October 21, 2021
Programming
440
0
Share
The Importance of Being Tested
Slides of the talk I gave at Droidcon Berlin 2021
Roberto Orgiu
October 21, 2021
More Decks by Roberto Orgiu
See All by Roberto Orgiu
Wellness & Droid
tiwiz
0
130
Behind the curtains
tiwiz
0
83
An Android Dev start to Kotlin MPP
tiwiz
0
200
Fantastic API and where to find them
tiwiz
0
92
Flipping the Koin @ GDG Dev Party
tiwiz
1
78
Flipping the Koin
tiwiz
2
180
Trip into the async world @ NYC Kotlin Meetup
tiwiz
0
130
Trip into the async world
tiwiz
1
150
GraphQL IRL (Android Makers)
tiwiz
0
160
Other Decks in Programming
See All in Programming
oxlintはeslint/typescript-eslintを置き換えられるのか
shomafujita
2
210
[BalkanRuby 2026] Drop your app/services!
palkan
3
670
CLIであることを活かしたGitHub Copilot CLI活用術 / GitHub Copilot CLI Pro Tips & Tricks
nao_mk2
1
990
今さら聞けないCancellationToken
htkym
0
180
UaaL×Androidアプリのメモリ計測 — Memory Profilerの先へ
rio432
0
180
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
950
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
290
誰も頼んでない機能を出荷した話
zekutax
0
130
AI 時代のソフトウェア設計の学び方
masuda220
PRO
15
5.4k
Transactional Change Stream Processing With Debezium and Apache Flink
gunnarmorling
1
130
Moments When Things Go Wrong
aurimas
3
110
運用エージェントは "作る" から "育てる" へ - 記憶と自己進化の3層設計パターン / self-evolving-agents-three-layer-agent-design
gawa
10
1.3k
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
46
8.2k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
300
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
360
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1.1k
KATA
mclloyd
PRO
35
15k
Believing is Seeing
oripsolob
1
130
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.4k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
For a Future-Friendly Web
brad_frost
183
10k
Transcript
Roberto Orgiu | Senior Android Engineer @ NYTimes | @_tiwiz
The importance of being tested
What is testing about?
Correctness Functioning
Is everything testable?
class Repository { private val dep = Dependency( ) fun
fetch() = dep.getData( ) }
class Repository ( private val dep: Dependenc y ) {
fun fetch() = dep.getData( ) }
What should I test?
Test the logic, not the code. Fabio Collini (probably?)
fun testCode() { repository.fetch( ) verify(mockDependency).getData( ) }
fun testLogic() { val actualResult = repository.fetch( ) verify(actualResult )
.isEqualTo(expectedResult ) }
Was that unit testing?
Was that unit testing? Is unit testing enough?
cwti.link/twitch
What about integration testing?
What about integration testing? integration tests validate the collaboration and
interaction of a group of units.
My take on integration testing
My take on integration testing •No Android deps •Test fl
ow from start to end •Use Robolectric
class RootFragment : Fragment() { lateinit var repository: Repositor y
lateinit var view: Vie w fun onResume() { val response = repository.fetchThings( ) view.bindResults(response ) } }
class IntegrationTest { private val mock = TestDouble(Service() ) private
val testFragment = RootFragment( ) fun integrationTest() { run(testFragment).verify( ) } } androidTest
What about Network?
What about network? Network is unreliable
What about network? • Retro fi t + OkHttp +
MockWebServer • Ktor + MockEngine
val retrofit = Retrofit.Builder( ) .baseUrl("https://api.github.com/" ) .build( ) This
should come from the outside!
val retrofit = Retrofit.Builder( ) .baseUrl(url ) .build( )
val server = MockWebServer( ) server.enqueue ( MockResponse().setBody("hello, world!" )
) server.start( ) val url = server.url( )
HttpClient(Android) { install(Logging) { … } install(JsonFeature) { … }
} This should come from the outside!
val mockEngine = MockEngine { request - > respond (
content = ByteReadChannel(content) , status = HttpStatusCode.OK , headers = headersOf(ContentType, type ) ) }
What about UI testing?
What about UI testing?
@get:Rule var activityScenarioRule = activityScenarioRule<MyActivity>( ) @Tes t fun changeText()
{ onView(withId(startViewId) ) .perform ( typeText(MESSAGE) , closeSoftKeyboard( ) ) onView(withId(buttonViewId) ) .perform(click() ) onView(withId(targetView) ) .check(matches(withText(MESSAGE)) ) }
None
None
@Composabl e fun SimpleUI() { var clicks by remember {
mutableStateOf(0) } Column { Button ( onClick = { clicks++ } ) { Text(text = "Click me" ) } if (clicks > 0) { Text(text = "$clicks" ) } } }
@get:Rul e val composeTestRule = createComposeRule( ) @Tes t fun
verify_initial_case() { composeTestRule.setContent { SimpleUI( ) } composeTestRule.onNodeWithTag("clicks" ) .assertDoesNotExist( ) }
@get:Rul e val composeTestRule = createComposeRule( ) @Tes t fun
verify_last_case() { composeTestRule.setContent { SimpleUI( ) } composeTestRule.onNodeWithText("Click me" ) .performClick( ) with(composeTestRule.onNodeWithTag("clicks")) { assertIsDisplayed( ) assertTextEquals("1" ) } }
How can I start testing?
Roberto Orgiu | Senior Android Engineer @ NYTimes | @_tiwiz
Thanks for listening. Q&A Time