【问题标题】:How do I fix these Unit Tests?如何修复这些单元测试?
【发布时间】:2021-12-20 08:00:40
【问题描述】:

我正在为 Uni 做课程作业,我真的在为完成之前的最后一项任务而苦苦挣扎:

使用任何适当的方法(Bash 脚本、JUnit、Maven、Gradle 等)实施一组单元测试,确认已满足任务 5 中的要求。 这些测试应该由 Jenkins 作为单个作业的一部分自动执行,该作业运行所有测试并仅在任何测试未通过时导致作业失败。 部分解决方案将获得相应的分数。

任务 5 任务是

  1. 没有输入参数返回错误
  2. 错误处理以确保非整数输入不会被转换但不会导致构建失败。

代码如下:

class Dec2Hex {

    public static int Arg1;

    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.print("No input detected. Try Again.\n");
            return;
        }



        try {
            Arg1 = Integer.parseInt(args[0]);
            //parseInt succeeded
        } catch(NumberFormatException e)
        {
            //parseInt failed
        }

        if (Arg1 <= 0) {
            System.out.println("Value must be a positive Int");
            return;
        }

        char ch[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int rem, num;
        num = Arg1;
        String hexadecimal = "";
        System.out.println("Converting the Decimal Value " + num + " to Hex...");

        while (num != 0) {
            rem = num % 16;
            hexadecimal = ch[rem] + hexadecimal;
            num = num / 16;
        }

        System.out.println("Hexadecimal representation is: " + hexadecimal);
        System.out.println("Ending Program");

    }

以及我作为测试类所拥有的:

import static org.junit.jupiter.api.Assertions.*;

class Dec2HexTest {

    @org.junit.jupiter.api.Test
    void main() {
        Dec2Hex.main(new String[] {"16"});
        Dec2Hex.main(new String[] {"641"});
        Dec2Hex.main(new String[] {"16541"});
        Dec2Hex.main(new String[] {"asdasd"});
    }
}

现在,我知道这是错误的,但我的大脑已经转向汤,试图弄清楚如何做到这一点,并且该周五到期。我不是在寻找一个完整的解决方案,只是为了让我朝着正确的方向前进,这样我就可以完成自己,因为我找不到任何涵盖类似内容的教程。

【问题讨论】:

  • 欢迎来到 Stack Overflow。我们需要更多关于问题所在的具体信息。实际问题是什么?你想断言什么?正确的方向是什么?另请阅读:How do I ask and answer homework questions?
  • 这是一个 QA 网站,而不是一个指导人们的网站。也就是说:尝试将相关代码从main 方法移动到其他方法(由main 调用)。然后用 JUnit 测试这些方法。

标签: java maven junit


【解决方案1】:

根据任务描述,单元测试如下所示:

import static org.junit.jupiter.api.Assertions.assertThrows;

public class MainTest {

    @org.junit.jupiter.api.Test
    public void shouldThrowException_GivenNoInputArgument() {
        assertThrows(Exception.class, () -> {
            Main.main(new String[] {});
        });
    }

    @org.junit.jupiter.api.Test
    public void shouldNotThrowException_GivenNonIntegerArgument() {
        Main.main(new String[] {"non-integer-argument"});
    }

}

如果你尝试这个,你会注意到第一个失败,因为在这种情况下你没有抛出异常。所以你必须相应地修复你的实现。

此外,您确定要将所有代码都包含在您的 main() 方法中吗?我猜不会。考虑定义类,以便您可以轻松地测试它们中的每一个。

最后,我建议你阅读一些有关单元测试的资料,以更好地理解这个概念:

  1. https://www.manning.com/books/unit-testing
  2. https://betterprogramming.pub/13-tips-for-writing-useful-unit-tests-ca20706b5368
  3. https://www.baeldung.com/junit-5

【讨论】:

  • 谢谢!这正是我一直在寻找的,我现在也在重构代码,所以我一定会尝试扩展你向我展示的内容。
猜你喜欢
  • 2020-05-20
  • 2018-03-13
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 2017-11-27
  • 2021-02-12
相关资源
最近更新 更多