Sobes.tech
Junior — Middle

What tools did you use to develop mock contexts in your projects?

sobes.tech AI

Answer from AI

For developing mock contexts in Android projects, I often use the following tools:

  • Mockito — for creating mocks and stubs of objects, allowing to simulate the behavior of dependencies.
  • Robolectric — for running tests that require an Android context without the need for an emulator.
  • MockK — a modern library for mocking in Kotlin, convenient for mocks of final classes and functions.
  • AndroidX Test Core — contains the ApplicationProvider.getApplicationContext() class, which can be used to obtain the context in tests.

Example of creating a mock context with Mockito:

Context mockContext = Mockito.mock(Context.class);
when(mockContext.getString(R.string.app_name)).thenReturn("Mocked App Name");

For a more realistic context, Robolectric can be used, which provides a full Android context in JVM tests.