如何使用AndroidJUnitRunner运行JUnit测试
我想为一些只需要Android上下文的代码编写一个JUnit测试,但我不想要模拟,我想要一个真正的上下文传递给我的对象如何使用AndroidJUnitRunner运行JUnit测试,android,unit-testing,testing,Android,Unit Testing,Testing,我想为一些只需要Android上下文的代码编写一个JUnit测试,但我不想要模拟,我想要一个真正的上下文传递给我的对象 我想初始化一个领域实例。下面的例子是google教程 它展示了如何创建使用模拟上下文对象的单元测试 要使用此框架将模拟对象添加到本地单元测试,请遵循以下编程模型: import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.*; import stati
我想初始化一个领域实例。下面的例子是google教程 它展示了如何创建使用模拟上下文对象的单元测试 要使用此框架将模拟对象添加到本地单元测试,请遵循以下编程模型:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import android.content.SharedPreferences;
@RunWith(MockitoJUnitRunner.class)
public class UnitTestSample {
private static final String FAKE_STRING = "HELLO WORLD";
@Mock
Context mMockContext;
@Test
public void readStringFromContext_LocalizedString() {
// Given a mocked Context injected into the object under test...
when(mMockContext.getString(R.string.hello_word))
.thenReturn(FAKE_STRING);
ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);
// ...when the string is returned from the object under test...
String result = myObjectUnderTest.getHelloWorldString();
// ...then the result should be the expected one.
assertThat(result, is(FAKE_STRING));
}
}
我正在尝试初始化领域实例,因此无法使用模拟上下文