简体   繁体   English

测试(junit)switch 语句中的默认情况?

[英]Testing (junit) the default case in a switch statement?

I need to know how to test the default case in a switch statement with junit.我需要知道如何使用 junit 在 switch 语句中测试默认情况。 I can't change the code itself and I'm trying for 100% coverage but I don't know how to test my default.我无法更改代码本身,我正在尝试 100% 的覆盖率,但我不知道如何测试我的默认值。 Helps?有帮助吗?

public Hello helloSwitch() {
        Hello hi = Hello.A;
        switch (this) {
        case A:
            hi = Hello.B;
            break;
        case B:
            hi = Hello.C;
            break;
        case C:
            hi = Hello.A;
            break;
        default:
            hi = Hello.A;
            break;
        }

I had to modify the code a fair bit so sorry that it looks silly.我不得不稍微修改代码,很抱歉,它看起来很傻。 I just need to know how to write a junit to test the default, I've tested everything else.我只需要知道如何编写一个 junit 来测试默认值,我已经测试了其他所有内容。

I can't change this code.我无法更改此代码。

Edit: changed编辑:改变

Edit: This code isn't important I jut need to know how to write the unit test for the default编辑:这段代码并不重要,我只需要知道如何为默认值编写单元测试

Edit: I can't change, the code itself, I'm only writing the tests.编辑:我无法更改代码本身,我只是在编写测​​试。 I need 100% coverage though.不过我需要 100% 的覆盖率。

Let me assume that 'hallo' is a variable set somewhere outside the given method.让我假设 'hallo' 是在给定方法之外某处设置的变量。 Let me further assume that the Enum type currently only allows values present in the switch statement.让我进一步假设 Enum 类型当前只允许存在于 switch 语句中的值。 In this case you can use "null" to trigger the default case.在这种情况下,您可以使用“null”来触发默认情况。 In this case the default statement is unreachable and should not be there at all.在这种情况下,默认语句无法访问,根本不应该存在。 While there might be ways to still "test" this - meaning to execute the code running a test - this would not add any benefit.虽然可能有办法仍然“测试”这一点 - 意味着执行运行测试的代码 - 这不会增加任何好处。 If you have more enum constants available than pick any covered by the default case.如果您有更多可用的枚举常量,而不是选择默认情况下涵盖的任何枚举常量。

As some already have mentioned:正如一些人已经提到的:

  • dead code cannot and should not be tested but removed.死代码不能也不应该被测试,而是被删除。
  • 100% test coverage sounds nice but usually is not a realistic or sensible goal 100% 的测试覆盖率听起来不错,但通常不是一个现实或合理的目标
  • in my opinion test shouldn't even know about the code in a method but test the method as a black box.在我看来,测试甚至不应该知道方法中的代码,而是将方法作为黑盒进行测试。

visit this link can help you : [JUnit is a standardized framework for testing Java units (that is, Java classes).访问此链接可以帮助您: [JUnit 是用于测试 Java 单元(即 Java 类)的标准化框架。 JUnit can be automated to take the some of the work out of testing. JUnit 可以自动完成一些测试工作。

Imagine you've created an enum type with three values: GREEN, YELLOW, and RED.假设您创建了一个具有三个值的枚举类型:GREEN、YELLOW 和 RED。 Listing 1 contains the code: http://www.dummies.com/programming/java/using-junit ] 1清单 1 包含代码: http://www.dummies.com/programming/java/using-junit ] 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM