我正在尝试按照此处的示例运行简单的单元测试:
https://developer.android.com/training/testing/unit-testing/local-unit-tests
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Test;
import static com.google.common.truth.Truth.assertThat;
public class UnitTestSampleJava {
private static final String FAKE_STRING = "HELLO_WORLD";
private Context context = ApplicationProvider.getApplicationContext();
@Test
public void readStringFromContext_LocalizedString() {
// Given a Context object retrieved from Robolectric...
ClassUnderTest myObjectUnderTest = new ClassUnderTest(context);
// ...when the string is returned from the object under test...
String result = myObjectUnderTest.getHelloWorldString();
// ...then the result should be the expected one.
assertThat(result).isEqualTo(FAKE_STRING);
}
}
我有一个全新的项目,我按照指定设置了 gradle 文件,然后我用这一行创建了一个测试:
private Context context = ApplicationProvider.getApplicationContext();
我在该行号上得到一个例外,说明:
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
但是,这在文档中被列为本地单元测试,而不是仪器测试。