gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:11:47 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com