dependencies {
implementation 'com.example:mylibrary:1.0.0'
androidTestImplementation 'com.example:mylibrary:1.0.0'
}
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);
}
}
在 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);