You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio中为Java模块启用单元测试?

Setting Up Unit Tests for Your Android Java Modules

Hey there! Since you're new to Android development and want to add unit tests to your two Java modules, let's break down exactly how to get this up and running—no fancy tricks required.

1. First: Configure Gradle for Testing

Each of your Java modules needs the right dependencies to run unit tests. Open the build.gradle file for each module and update the dependencies block:

dependencies {
    // Core JUnit 4 library for writing tests
    testImplementation 'junit:junit:4.13.2'
    
    // Optional: Mockito for mocking objects (great if you need to fake dependencies)
    testImplementation 'org.mockito:mockito-core:4.11.0'
}

If your modules are Android library modules (not just plain Java modules), add this inside the android block to handle Android SDK-related classes during testing:

android {
    // ... your existing module config
    
    testOptions {
        unitTests.returnDefaultValues = true
        // This lets you use Android classes in tests without needing a device/emulator
    }
}

2. Set Up the Test Directory Structure

Android Studio usually creates this automatically, but if it's missing, here's what to do:

  • For each module, create a src/test/java directory
  • Mirror your main code's package structure inside this folder. For example, if your main code lives in src/main/java/com/example/myapp/module1, your tests go in src/test/java/com/example/myapp/module1
  • Name your test classes like [YourClassName]Test (e.g., UserServiceTest for a UserService class)—this is a standard convention that makes tests easy to find

3. Write Your First Unit Test

Let's use a simple example to get started. Suppose you have a MathUtils class in one of your modules:

public class MathUtils {
    public static int multiply(int x, int y) {
        return x * y;
    }
}

Create a corresponding MathUtilsTest class in your test directory:

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class MathUtilsTest {

    @Test
    public void multiply_positiveNumbers_returnsCorrectProduct() {
        // Arrange: Set up your test data and objects
        int num1 = 4;
        int num2 = 5;
        
        // Act: Run the method you're testing
        int result = MathUtils.multiply(num1, num2);
        
        // Assert: Verify the result is what you expect
        assertEquals(20, result);
    }
}

Key points here:

  • Use the @Test annotation to mark methods that are test cases
  • Follow the Arrange-Act-Assert pattern—it makes tests easy to read and debug

4. Run Your Tests

You have two easy ways to run your unit tests:

  • In Android Studio: Right-click on a test class or method, then select "Run [TestClassName]"—you'll see results in the "Run" window (green checkmarks = passes, red Xs = failures)
  • Via Gradle Command Line: From your project root, run ./gradlew :your-module-name:testDebugUnitTest (replace your-module-name with the actual name of your module, like :module1:testDebugUnitTest). To run tests for all modules at once, use ./gradlew test

5. Tips for Multi-Module Projects

  • Each module's tests are independent—configure dependencies and write tests separately for each one
  • If one module depends on another (e.g., Module A uses code from Module B), your tests for Module A will automatically have access to Module B's classes, no extra setup needed
  • Keep tests focused: Each test should verify one specific behavior of your code—this makes it easier to pinpoint issues when tests fail

内容的提问来源于stack exchange,提问作者mownier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:23:31