Getting started with testing in Android in 2023 is easier than ever before. We will be covering some fundamentals of thoughts around testing as well as getting into actual examples of how to test your code.
var example: Example @Before fun setUp() { example = Example() } @Test fun `state can be changed` () { assertNull( example.state) example.state = 1_000L assertEquals( 1_000L, example.state) }
values to method calls. It always returns the same output regardless of the input… Mock A mock is similar to a stub, but the behaviour of the mocked interface can be changed dynamically based on scenarios… Fake A Fake is an object with a concrete implementation that works similarly to the actual implementation… Spy A spy lets us verify what functions were called, with what arguments, when, and how often… Test double, Dummy…
} class Example: ExampleRepo { override fun one() = 1 } class ExampleTest { private lateinit var example: Example private lateinit var exampleRepo : ExampleRepo @Before fun setUp() { example = Example() exampleRepo = object: ExampleRepo { override fun one(): Int = 1 } } }
Database (Room) -> In memory / fake - Networking (OkHttp + Retrofit) -> Mock server + canned responses - Local storage -> Mock repos or clear before each test
Database (Room) -> In memory / fake - Networking (OkHttp + Retrofit) -> Mock server + canned responses - Local storage -> Mock repos or clear before each test - Local files -> Mock repos or clear before each tests
Database (Room) -> In memory / fake - Networking (OkHttp + Retrofit) -> Mock server + canned responses - Local storage -> Mock repos or clear before each test - Local files -> Mock repos or clear before each tests - Other -> Mock