Skip to content
:tech
← Handbook

AI-translated. Read the original in KoreanEmbedded slides, videos and images remain in their original language.

Android Testing

This document provides Android testing methods and guidelines, including recommended libraries and how to use them. These are recommendations only; projects are free to adopt or propose other approaches. Even then, we encourage consistency within each project.

Testing Frameworks and Libraries

Use the libraries below for Android testing. You may add other libraries, but avoid adopting more powerful tools such as PowerMock solely to use features outside the testing guidelines, such as mocking static methods.

JUnit 4

https://junit.org/junit4/

This is our Android testing framework. Although JUnit 5 has been released, using it on Android requires additional setup, so we still use the JUnit 4 framework provided by default.

Mockito 2

http://site.mockito.org/

Mockito is a mocking library. It lets you build a test environment by mocking objects that the object under test depends on. It cannot mock static methods or final classes. PowerMock makes those operations possible, but we discourage its use: a need to mock static methods often indicates a problem with the code’s design or the testing approach.

Robolectric

http://robolectric.org/

Robolectric simulates Android framework behavior, allowing tests that need activities, views, or similar components to run without an Android emulator or physical device. It is useful when unit tests must use the Android framework. Although Robolectric 4 has been released, this guide is based on Robolectric 3.

Unit Tests

For basic unit-testing guidelines, see Testing Principles.

Running Tests

gradle Console

Run tests with gradlew. Specify a test method to run only the tests you want.

# 특정 테스트 실행
./gradlew test --tests com.buzzvil.locker.SomeTest.someSpecificFeature
# 특정 패키지 테스트 실행
./gradlew test --tests all.in.specific.package*

Android Studio

Place the cursor in the test method or class you want to run, then press Control + Shift + D. You can also click the icon to the left of the test method.

icon-test

Writing Test Code

Naming Guidelines

Follow the Javatests style rules. Prefix the method under test with test and use camel case. Separate the test scenario with an underscore (_), also writing the scenario in camel case.

@Test
public void testIsCampaignFilteredOut_noFilter() {
    ...
}

Import Guidelines

Use static imports for testing methods.

// 임포트 이후 `Mockito.mock` 대신 `mock` 을 바로 사용한다.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Dependency Mocking Guidelines

Use dependency injection to supply mocked dependencies.

class CampaignFilter {
    CampaignFilter() {
        this(PreferenceHelper.getString(PrefKey.FILTERED_AD, "").split(DELIMITER),
                PreferenceHelper.getString(PrefKey.FILTERED_CONTENT, "").split(DELIMITER));
    }

    CampaignFilter(final String[] filteredAds, final String[] filteredContent) {
        this.filteredAds = filteredAds;
        this.filteredContent = filteredContent;
    }

    // ...
}
class CampaignFilterTest {
    @Test
    public void testIsCampaignFilteredOut_noFilter() {
        // 필터가 없는 경우를 테스트한다.
        final CampaignFilter campaignFilter = new CampaignFilter(new String[]{}, new String[]{});
        ...
    }
}

Mocking the Android Framework

Activity Lifecycle

Use Robolectric to test the activity lifecycle.

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class VideoOverlayActivityTest {
    @Test
    public void testOnCreate() {
        final VideoOverlayActivity activity = Robolectric.buildActivity(
            VideoOverlayActivity.class, getDefaultIntent()).create().get();

        assertThat(activity.isFinishing(), is(false));
    }
}

Shadowing

Avoid mocking static methods whenever possible. Likewise, inject singleton dependencies rather than creating them directly inside a class. When existing code makes this unavoidable, use Robolectric shadows.

@Implements(SomeSingleton.class)
public class ShadowSomeSingleton {
    static SomeSingleton someSingleton;

    @Implementation
    public static SomeSingleton getInstance() {
        if (someSingleton == null) {
            someSingleton = mock(SomeSingleton.class);
        }

        return someSingleton;
    }
}
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, shadows = ShadowSomeSingleton.class)
public class SomeActivityTest {
    // ...
}

References

All guides
Handbook

Android Modules

This document describes considerations when building Android modules. These are recommendations; individual projects and modules may use different structures. Guidelines - Write ne…

Handbook

Multi-Module Android SDKs

Introduction When an app is modularized, it can reference each module as a local library. You can then version only the app and distribute the APK without managing separate module…