java - JUnit 测试用例未正确执行

标签 java junit

我正在尝试使用 JUnit 执行一个简单的测试用例,但即使在错误条件下,测试用例也总是通过。我无法找出下面程序中的错误。 Scanner 类多个输入似乎在某个地方存在错误。

我想使用测试用例的多个输入进行测试,以便测试用例接受不同的员工类型。

import java.util.Scanner;

import static org.junit.Assert.assertEquals;

public class EmployeeIdentificationTest extends TestCase {
    String firstName, middleName,lastName;
       int age=0;

       String choice=null;

       // assigning the values
       protected void setUp(){
          do{
            Scanner scanner = new Scanner(System.in);

           System.out.println("Enter  firstName  ");
           firstName= scanner.nextLine();
           System.out.println("Enter middleName");
           middleName= scanner.nextLine();
           System.out.println("Enter lastName");
           lastName= scanner.nextLine();
           System.out.println("Enter the age");
           int flag=0; 
           while(flag==0){
            try{
               age= scanner.nextInt();
               flag=1;
               }
               catch(Exception e){
               scanner.nextLine();
                System.out.println("Wrong entry, please enter digits only:");
            }
          }

        System.out.println("Do you like to fetch more records press Yes or No");
       scanner.nextLine();   
       choice=scanner.nextLine();
       }while(choice.contains("Y")||choice.contains("y"));      
       }
       // test method 
       public void testEmployee(){
       String employeeType=EmployeeIdentification.getEmployeeType(firstName, middleName, powerTrain, age);
       if(employeeType.equals("Junior Developer")||employeeType.equals("Senior Developer")||employeeType.equals("System Analyst")||employeeType.equals("Manager")||employeeType.equals("Delivery Head"))
       assert(true);
       }


}

最佳答案

Ldvg 的有效观点,我认为你的意思是:

public void testEmployee(){
    String employeeType = EmployeeIdentification.getEmployeeType(firstName, middleName, powerTrain, age);
    if(employeeType.equals("Junior Developer")||
        employeeType.equals("Senior Developer")||
        employeeType.equals("System Analyst")||
        employeeType.equals("Manager")||
        employeeType.equals("Delivery Head")) {
        assert(true);
    } else {
        assert(false);
    }
}

关于java - JUnit 测试用例未正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36085773/

相关文章:

java - 如何将 OnClickListener 放入 IF 语句中?

java - 在 Ubuntu 上安装 Jetty,获取 com.sun.tools.javac.Main 不在类路径上

java - 拦截器在 Spring Boot GraphQL 中不起作用

java - Maven递归依赖排除

junit - 在 JUnit 5 中使用枚举值作为标签

java - 使用字符退出程序

java - Java 6 在 JDK、JVM 或两者中的性能改进是什么?

java - 为单个测试用例生成 JUnit 报告

java - 使一定百分比的 JUnit 测试通过

testing - Jersey 测试 - 带有 JSON 请求的 Http Delete 方法