java - 我做错了什么?安卓JUnit

标签 java android junit

我想留在junit。并创建一些测试应用程序,然后尝试测试。我有简单的 Activity ,在这个 Activity 中我有方法 checkEditText 该方法返回 true 或 false。这是我的 Activity :

 @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)

最佳答案

因为0不大于0。所以

return number > 0 ? true : false;

当 number 为 0 时返回 false。

在测试中,您断言 true,但结果为 false。

关于java - 我做错了什么?安卓JUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37531348/

相关文章:

java - 私有(private)字符串还是公共(public)静态字符串?

java - 在 Java 中打印 BufferedImage

java - Spring Cloud 配置: define server config properties programmatically

android - ListView 中的 ImageView

java - 为什么 JUnit 套件类不执行自己的 Test、Before 和 After 注释?

java - 从文本文件中检测数组

android - Android Speech Recognition Activity的启动延迟

android - 闪屏上的沉浸式模式

eclipse - 无法运行包含来自 Eclipse 的线程的 JUnit 测试用例

java - 运行所有测试用例并忽略junit中的@ignore注释