我想要堅持不懈。並創建一些測試應用程序,然後嘗試測試。我有簡單的活動,並且在此活動中我有方法checkEditText
此方法返回true或false。這是我的活動:我做錯了什麼? Android JUnit
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean checkEditText(int number) {
return number > 0 ? true : false;
}
這是測試:
package ac.junit2;
import android.test.ActivityInstrumentationTestCase2;
import org.junit.Assert;
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
MainActivity activity = getActivity();
Assert.assertNotNull(activity);
}
public void testCheckNumber() {
MainActivity activity = getActivity();
boolean action = activity.checkEditText(0);
assertTrue(action);
}
}
我有錯誤:
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertNotNull(Assert.java:712)
at org.junit.Assert.assertNotNull(Assert.java:722)
at ac.junit2.MainActivityTest.setUp(MainActivityTest.java:26)
at junit.framework.TestCase.runBare(TestCase.java:139)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
嗯,這不是像答案建議的編輯文本。 'MainActivityTest.setUp'中的'Assert.assertNotNull'已經失敗。我不知道'getActivity()'應該如何工作,但它顯然沒有給你帶來任何東西。 – zapl
我想知道的另一件事:你不需要在你的測試方法中使用'@ Test'註釋嗎? Junit應該如何知道它應該調用'testCheckNumber()'? – GhostCat