【问题标题】:Compare AssertNull in an if statement [closed]在 if 语句中比较 AssertNull [关闭]
【发布时间】:2020-08-01 01:21:32
【问题描述】:

在我的 JUnit 测试断言语句中,如果测试通过,我想打印一些东西。它当前抛出错误,因为它返回类型 void?有没有可行的方法?

import java.util.Arrays;
import org.junit.Test;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.Calendar;

import static org.junit.Assert.*;

public class Main {
    public static void main(String[] args) {
        Object x = b();
        if (assertNull(x) == null) {
            System.out.println("123");
        }
    }
    public static Object b(){
        return null;
    }
}

【问题讨论】:

  • 这不是 JUnit 测试的编写方式!
  • 如果是junit test为什么还要写main方法?
  • 我知道,我将这段代码作为示例从我的代码中导出,这样人们就可以将它粘贴进去并尝试快速使用它@Seelenvirtuose

标签: java junit compiler-errors assertion


【解决方案1】:

如果参数不是nullassertNull 会以失败终止测试。如果你想在测试通过的情况下打印一些东西,你可以在assertNull之后得到它:

assertNull(x);
// If x is not null, the test ends here, and the following statement isn't reached
System.out.println("123");

【讨论】:

    猜你喜欢
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2013-12-02
    • 1970-01-01
    相关资源
    最近更新 更多