Back to Repositories

Implementing Base Android Test Infrastructure in AndroidUtilCode

This base test class provides fundamental test infrastructure for the AndroidUtilCode library, implementing Robolectric for Android unit testing. It establishes core testing capabilities with shadow classes and application context initialization.

Test Coverage Overview

The BaseTest class serves as a foundation for Android utility testing, providing essential test infrastructure setup.

Key aspects include:
  • Application context initialization through RuntimeEnvironment
  • Shadow class implementation for Android component simulation
  • System output stream configuration for logging
  • Basic test method scaffolding

Implementation Analysis

The testing approach utilizes Robolectric’s test runner and shadow classes for Android environment simulation.

Technical implementation features:
  • RobolectricTestRunner configuration for Android framework testing
  • ShadowLog implementation for test output management
  • Utils initialization with mock application context
  • Constructor-based test environment setup

Technical Details

Testing infrastructure components:
  • JUnit 4 test framework
  • Robolectric for Android environment simulation
  • Shadow classes for Android component mocking
  • RuntimeEnvironment for application context provision
  • Config.NONE manifest configuration

Best Practices Demonstrated

The test implementation showcases several testing best practices in Android development.

Notable practices include:
  • Proper test runner configuration
  • Effective shadow class utilization
  • Clean separation of test setup logic
  • Structured base class implementation for test reuse
  • Appropriate logging configuration

blankj/androidutilcode

lib/subutil/src/test/java/com/blankj/subutil/util/BaseTest.java

            
package com.blankj.subutil.util;


import com.blankj.utilcode.util.Utils;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog;


/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/08/03
 *     desc  :
 * </pre>
 */
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, shadows = {ShadowLog.class})
public class BaseTest {

    public BaseTest() {
        ShadowLog.stream = System.out;
        Utils.init(RuntimeEnvironment.application);
    }

    @Test
    public void test() throws Exception {
    }
}