Test support
UpdatedThere's a new version available!
These pages cover version 3 of our SDK, but a newer version is available. In general, we suggest that you update to the latest version to take advantage of new features and fixes.
- Are you new to our SDKs? Check out the latest docs.
- Otherwise, learn about updating to the latest version
The SDK makes it easy to write unit, integration, UI, or other types of automated tests in your code base. We designed our SDK with first-class support for automated testing, making it easy to inject dependencies and perform mocking in your code.
This page is part of a setup flow for the SDK. Before you continue, make sure you've implemented previous features—i.e. you can't receive in-app notifications before you identify people!
Dependency injection
Every SDK class inherits from an interface. Inherited interfaces use a consistent naming convention: <NameOfClass>Instance
. For example, the CustomerIO
class inherits the protocol CustomerIOInstance
.
If you want to inject a class in your project, it could look something like the example below.
class ProfileRepository(private val cio: CustomerIOInstance) {
// Now, you can call any of the `CustomerIO` class functions with `this.cio`!
suspend fun loginUser(email: String, password: String) {
// Login the user to your system...
// Then, identify the profile with Customer.io:
cio.identify(email)
}
}
val cio = CustomerIO.instance()
val repository = ProfileRepository(cio)
Mocking
Every SDK class inherits from an interface. Inherited interfaces use a consistent naming convention: <NameOfClass>Instance
. For example, the CustomerIO
class inherits the protocol CustomerIOInstance
.
Interfaces are really easy to mock with mocking frameworks such as Mockito.
Here’s an example test class showing how you would test your ProfileRepository
class.
class ProfileRepositoryTest {
private val cioMock: CustomerIOInstance = mock()
private lateinit var repository: ProfileRepository
@Before
fun setUp() {
repository = ProfileRepository(cioMock)
}
@Test
fun test_loginUser(): Unit = runBlocking {
val givenEmail = "example@example.com"
val givenPassword = "123"
// Now, call your function under test:
repository.loginUser(givenEmail, givenPassword)
// You can access many properties of the mock class to assert the behavior of the mock.
verify(cioMock, times(1)).identify(givenEmail, givenPassword)
}
}