Java 如何为switch case()语句编写junit测试用例?

Java 如何为switch case()语句编写junit测试用例?,java,Java,我正在使用JUnit编写switch()case语句。我是JUnit环境的新手。我真的被困在如何使用JUnit编写switch()案例中 我真的很想知道怎么做 提前谢谢 比较:比较类型表示两个表达式之间的比较。比较类型应包含以下枚举类型,以表示语言的可能比较(如果愿意,您可以自由添加其他运算符): 运算符表示,>=,==,和=操作。 应使用三个参数创建比较类型实例:来自上述运算符枚举类型的操作,以及表示左操作数表达式和右操作数表达式的两个表达式。该类型应允许任何具有int/Integer值的语言

我正在使用JUnit编写
switch()
case语句。我是JUnit环境的新手。我真的被困在如何使用JUnit编写
switch()
案例中

我真的很想知道怎么做

提前谢谢

比较:比较类型表示两个表达式之间的比较。比较类型应包含以下枚举类型,以表示语言的可能比较(如果愿意,您可以自由添加其他运算符):

运算符表示
,>=,==,
=操作。
应使用三个参数创建比较类型实例:来自上述运算符枚举类型的操作,以及表示左操作数表达式和右操作数表达式的两个表达式。该类型应允许任何具有
int/Integer
值的语言结构成为可能的表达式。比较类型应具有以下方法:

值:将状态作为输入并返回
布尔值/布尔值
值,该值是将操作应用于每个操作数表达式的值的结果。应该使用状态来获取表达式的值

import java.util.*;
import java.io.*;

/**
 * create a class called ArithmeticOperation with generic type and implements toString interface
 * @author a13875810600
 *
 * @param <I>
 */
public class ArithmeticOperation <I> implements ToString{

    /**
     * create a enum Operator class with special Operators
     * @author a13875810600
     *
     */
     public enum Operator {
     Add, Sub, Mult, Div, Rem;
}
    // create field called leftoperand
    private Number leftoperand;
    // create field called operation
    private  Operator operation;
    // create field called rightoperand
    private Number rightoperand; 

    /**
     * creation of the  constructor of the ArithmeticOperation
     * @param leftoperand
     * @param operation
     * @param rightoperand
     */
    public ArithmeticOperation(Number leftoperand, Operator operation,Number rightoperand){
          this.leftoperand = leftoperand;
          this.operation =  operation;
          this.rightoperand = rightoperand;         
     }



   /**
    *  Demonstrate all cases of the enmu type by applying operation Operator to the leftoperand and 
    *  rightoperand and get values of the, Because only five types of emu if errors will occur it is because the Human only 
          recognize five types of operators 
    * @param s
    * @return 
    */
    public Integer value(State s){

         Integer result = 0;
         switch (this.operation) {
              case Add:
                   result = leftoperand.value(s) + rightoperand.value(s);
              case Sub:
                   result = leftoperand.value(s) - rightoperand.value(s);
              case Mult:
                   result = leftoperand.value(s) * rightoperand.value(s);
              case Div:
                   result = leftoperand.value(s) / rightoperand.value(s);
              case Rem:
                   result = leftoperand.value(s) % rightoperand.value(s);                   
         }
         return result;
     }
     }
import java.util.*;
导入java.io.*;
/**
*使用泛型类型创建一个名为arithmetricOperation的类,并实现toString接口
*@作者a13875810600
*
*@param
*/
公共类算术运算实现ToString{
/**
*使用特殊运算符创建枚举运算符类
*@作者a13875810600
*
*/
公共枚举运算符{
加、分、多、分部、Rem;
}
//创建名为LeftOperator的字段
私有数左操作数;
//创建名为操作的字段
私人经营者经营;
//创建名为RightOperator的字段
私有数操作数;
/**
*创建算术运算的构造函数
*@param leftoperand
*@param操作
*@param rightOperator
*/
公共算术运算(数字左操作数、运算符运算、数字右操作数){
this.leftOperator=左操作数;
这个操作=操作;
this.rightOperator=rightOperator;
}
/**
*通过对LeftOperator应用操作运算符和
*RightOperator和get的值,因为只有五种类型的emu如果出现错误,那是因为只有人
识别五种类型的操作员
*@param s
*@返回
*/
公共整数值(状态s){
整数结果=0;
开关(此操作){
案例补充:
结果=左操作数.值+右操作数.值;
个案小组:
结果=左操作数.值-右操作数.值;
案例多:
结果=左操作数.值)*右操作数.值;
案件组:
结果=左操作数.值/右操作数.值;
案例Rem:
结果=左操作数.value(s)%右操作数.value(s);
}
返回结果;
}
}

如评论中所述,您需要在案例中添加中断语句


要测试交换机用例,您需要为每个操作员编写一个单独的测试用例。

您了解代码不好的原因吗?您的开关块中没有中断语句,但您确实需要它们,否则,您得到的每种情况都是:result=leftoperand.value(s)%righoperand.value(s);这是瀑布原理。对于单元测试,您有什么问题?您的代码无法编译,您将无法测试它。。。
import java.util.*;
import java.io.*;

/**
 * create a class called ArithmeticOperation with generic type and implements toString interface
 * @author a13875810600
 *
 * @param <I>
 */
public class ArithmeticOperation <I> implements ToString{

    /**
     * create a enum Operator class with special Operators
     * @author a13875810600
     *
     */
     public enum Operator {
     Add, Sub, Mult, Div, Rem;
}
    // create field called leftoperand
    private Number leftoperand;
    // create field called operation
    private  Operator operation;
    // create field called rightoperand
    private Number rightoperand; 

    /**
     * creation of the  constructor of the ArithmeticOperation
     * @param leftoperand
     * @param operation
     * @param rightoperand
     */
    public ArithmeticOperation(Number leftoperand, Operator operation,Number rightoperand){
          this.leftoperand = leftoperand;
          this.operation =  operation;
          this.rightoperand = rightoperand;         
     }



   /**
    *  Demonstrate all cases of the enmu type by applying operation Operator to the leftoperand and 
    *  rightoperand and get values of the, Because only five types of emu if errors will occur it is because the Human only 
          recognize five types of operators 
    * @param s
    * @return 
    */
    public Integer value(State s){

         Integer result = 0;
         switch (this.operation) {
              case Add:
                   result = leftoperand.value(s) + rightoperand.value(s);
              case Sub:
                   result = leftoperand.value(s) - rightoperand.value(s);
              case Mult:
                   result = leftoperand.value(s) * rightoperand.value(s);
              case Div:
                   result = leftoperand.value(s) / rightoperand.value(s);
              case Rem:
                   result = leftoperand.value(s) % rightoperand.value(s);                   
         }
         return result;
     }
     }