java - JUnit 测试返回 null 和 false

标签 java junit

所以我在 JUnit 中练习参数化测试。我创建了这个类:

public class Operation {

    public Operation() {

    }

    public int add(int value, int num) {
        int ans = value + num;
        return ans;
    }

    public int subtract(int value, int num) {
        int ans = value - num;
        return ans;
    }
}

没什么特别的,只是一些运行一些测试的方法。

我这里有这个测试类:

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class OperationTest {

    private int value;
    private int num;

    public OperationTest(int expected, int value, int num) {
        this.value = value;
        this.num = num;
    }

    @Parameterized.Parameters
    public static Collection primeNumbers() {
        return Arrays.asList(new Object[][] {
                { 4, 2, 2 },
                { 66, 6, 60 },
                { 20, 19, 1 },
                { 82, 22, 50 },
                { 103, 23, 80 }
        });
    }

    @Test
    public void test() {
        Operation o = new Operation();
        assertTrue(value == o.add(value, num));
    }

}

最后我有了一个类来运行我的测试:

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
    public static void main(String[] args) {
        Result result = JUnitCore.runClasses(OperationTest.class);
        for(Failure f : result.getFailures()) {
            System.out.println(f.toString());
        }

        System.out.println(result.wasSuccessful());
    }
}

当我运行它时,输出是:

test[0](OperationTest): null
test[1](OperationTest): null
test[2](OperationTest): null
test[3](OperationTest): null
test[4](OperationTest): null
false

我希望所有这些都是真的,因为 { 4, 2, 2 } 意味着期望 2 和 2 中的 4 作为参数提供给 Operation 类中的 add 方法...我猜这可能不是正确的方法...

非常感谢您的见解。

最佳答案

您没有在任何地方使用 expected - 甚至没有将其作为类状态的一部分。你在这里的断言:

assertTrue(value == o.add(value, num));

... 没有断言你想要它做什么。只有当num 为0 时,它才会起作用。仔细看。

你应该:

// final is optional here, of course - but that's what I'd do, anyway :)
private final int expected;
private final int value;
private final int num;

public OperationTest(int expected, int value, int num) {
    this.expected = expected;
    this.value = value;
    this.num = num;
}

然后:

assertTrue(expected == o.add(value, num));

或者更好(更清晰的诊断):

assertEquals(expected, o.add(value, num));

关于java - JUnit 测试返回 null 和 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21444848/

相关文章:

java - 如何在 JUnit 测试的方法内模拟方法调用?

java - 无法加载 OrientDB rw - "Cannot load database' s 配置。数据库似乎已损坏。”

java - Realm 和自动增量行为 (Android)

java - String.split ("(?<=\\G..)")在 Windows 和 Android 上分割不同

java - 从 ArrayList 获取字符串返回 null

文件系统的Java和Junit集成测试

java - Spring TransactionProxyFactoryBean 未加载 dao 服务

java - 当测试预期异常时,assertEquals 不会显示错误

android - 单元测试配置在 Android Studio 3.1 中损坏

Android 单元测试用例自动化 : Robolectric library vs Android Testing framework