我终于开始意识到对我的Android应用程序(特别是数据库操作)进行单元测试是个好主意,并找到了Robolectric框架来帮助我做到这一点。我在/app/src/test/java/<package>/SanityTest.java
中设置了一个简单的测试:
package <package>;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class SanityTest {
@Test
public void testTrueIsTrue() {
Assert.assertTrue(true);
}
}
我使用./gradlew robolectric
运行测试:
:app:compileDebugNdk UP-TO-DATE
:app:preBuild
// ...
:app:robolectricClasses UP-TO-DATE
:app:robolectric
<package>.SanityTest > testTrueIsTrue FAILED
java.lang.IllegalArgumentException
1 test completed, 1 failed
:app:robolectric FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:robolectric'.
> There were failing tests. See the report at: file:///<project_root>/app/build/reports/tests/index.html
Robolectric报告生成的堆栈跟踪显示IllegalArgumentException
其他一些信息:
我正在使用