我正在尝试使用 Robolectric 框架为我的 android 项目创建单元测试。我从 eclipse android 项目向导创建的一个空的 Android 项目开始。
在另一个 java 项目中,我使用此代码尝试运行基本测试:
@RunWith(RobolectricTestRunner.class)
public class ApiTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void shouldHaveApplicationName() throws Exception {
String appName = new RobolectricActivity().getResources().getString(R.string.app_name);
assertEquals(appName, "TestDemo");
}
@Test
public void testSomethingMeaningfulToMyApp() {
fail("not implemented");
}
}
Run Code Online (Sandbox Code Playgroud)
我已经按照Robolectric 快速入门指南中的说明配置了所有内容。
但是,当我尝试运行此测试类时,在访问 getResources() 函数的行中出现以下异常:
java.lang.RuntimeException: Did your shadow implementation of a method throw an exception? Refer to the bottom …Run Code Online (Sandbox Code Playgroud)