AndroidStudioChipmunk自动创建模块后无法在测试中解决生产代码
创始人
2024-10-11 04:30:22
0
  1. 检查生成的 Gradle 文件是否正确地设置了所需的依赖项。在 app 模块的 build.gradle 中,确保在 androidTestImplementation 行中包含生产代码所需的所有依赖项。例如:
dependencies {
    implementation 'com.example:mylibrary:1.0.0'
    androidTestImplementation 'com.example:mylibrary:1.0.0'
}
  1. 确保测试可以访问生产代码。在 app 模块中,确保生产代码可以在测试代码中正确引用。例如,
package com.example.myapp;

import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.runner.AndroidJUnit4;

import static org.junit.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("com.example.myapp", appContext.getPackageName());

        // Verify that we can instantiate our production class
        MyClass myClass = new MyClass();
        assertNotNull(myClass);
    }
}
  1. 如果仍然无法访问生产代码,则需要创建一个连接测试和生产代码的模块。在 app 模块中,创建一个新模块,例如 app-core,并在其 build.gradle 文件中引用 app 模块中的生产代码。例如,

在 app-core 模块的 build.gradle 文件中:

dependencies {
    implementation project(":app")
}

在测试中,引用生产代码时需要使用 app-core 模块的包名。例如,

package com.example.myapp.core;

import com.example.myapp.MyClass;

import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.runner.AndroidJUnit4;

import static org.junit.Assert.assertNotNull;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        MyClass myClass = new MyClass();
        assertNotNull(myClass);

相关内容

热门资讯

透视最新"wpk私人... 透视最新"wpk私人辅助"详细辅助透明挂教程(破解版内购技巧);支持2-10人实时对战,虚拟庄家发牌...
透视私人局"wepo... 透视私人局"wepoker透视功能下载"详细辅助玩家教你(私人局透视插件辅助);玩家必备必赢加哟《1...
透视有挂"约局吧开挂... 此外,数据分析德州(约局吧开挂神器是真的吗)辅助神器app还具备辅助透视行为开挂功能,通过对客户约局...
透视透视"wepok... 透视透视"wepoker破解版内购"详细辅助新2025版(如何设置透视攻略)1、许多玩家不知道wep...
透视ai代打"pok... 透视ai代打"pokemmo脚本辅助器"详细辅助必胜教程(私人局有透视神器),支持语音通讯、好友开房...
透视游戏"wepok... 透视游戏"wepoker私人辅助器"详细辅助插件教程(模拟器哪个教程)这是由厦门游乐互动科技有限公司...
透视挂透视"wpk显... 透视挂透视"wpk显示有作弊"详细辅助wpk教程(游戏下载软件);1.wpk显示有作弊 ai辅助创建...
透视好友"拱趴游戏破... 透视好友"拱趴游戏破解器"详细辅助透明挂教程(黑侠破解神器);拱趴游戏破解器软件透明挂更新新赛季,主...
透视免费"哈糖大菠萝... 透视免费"哈糖大菠萝有挂吗"详细辅助解密教程(透视挂底牌软件);实战中需综合运用上述技巧,并根据牌桌...
透视线上"we-po... 透视线上"we-poker靠谱吗"详细辅助技巧教程(透视脚本免费app方法);建议优先通过we-po...