gpt4 book ai didi

java - 从 JUnit 测试用例调用方法失败

转载 作者:行者123 更新时间:2023-12-01 12:14:25 31 4
gpt4 key购买 nike

我有一个问题:

我的 JUnit 测试用例中有这样的方法:

@Test
public void test1st_scenario() {

out = Mockito.mock(PrintStream.class);
System.setOut(out);
restaurant = new Restaurant();
employee = new Employee(null, null, null);

ArrayList<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("Luciano", "chef", ExperienceLevel.LOW));
employees.add(new Employee("Naved", "waiter", ExperienceLevel.LOW));
employees.add(new Employee("Fabrizio", "waiter", ExperienceLevel.LOW));
employees.add(new Employee("Amnir", "waiter", ExperienceLevel.LOW));
employees.add(new Employee("Abel", "barman", ExperienceLevel.LOW));

restaurant.setBudget(10000);
employee.increaseExperience(employees, 1, "Luciano");
Mockito.verify(out).println("The chef experience level increased");
Assert.assertEquals(8800, restaurant.getBudget());
}

Employee类中的increaseExperience方法是:

public void increaseExperience(ArrayList<Employee> employees,
int numberOfEmployee, String employeeName) {
restaurant = new Restaurant();
String[] parts = employeeName.split(",");
for (int i = 0; i < numberOfEmployee; i++)
{
for (int a = 0; a < employees.size(); a++)
{
if (employees.get(a).name.equals(parts[i]))
{
if (employees.get(a).job.equals("chef") || employees.get(a).job.equals("barman"))
{
if (employees.get(a).experience.equals(ExperienceLevel.LOW) || employees.get(a).experience.equals(ExperienceLevel.MEDIUM))
{
if (restaurant.getBudget() >= 1200)
{
restaurant.setBudget(restaurant.getBudget() - 1200);
if (i == 0)
System.out.println("The " + employees.get(a).job + " experience level is increased");
else
System.out.println(" and the " + employees.get(a).job + " experience level is increased");
}
else
{
if (i == 0)
System.out.println("The " + employees.get(a).job + " experience level is failed to increase");
else
System.out.println(" and the " + employees.get(a).job + " experience level is failed to increase");
}
}
else if (employees.get(a).experience.equals(ExperienceLevel.HIGH))
{
if (i == 0)
System.out.println("The " + employees.get(a).job + " experience level is increased");
else
System.out.println(" and the " + employees.get(a).job + " experience level is increased");
}
}
else if (employees.get(a).job.equals("waiter"))
{
if (employees.get(a).experience.equals(ExperienceLevel.LOW) || employees.get(a).experience.equals(ExperienceLevel.MEDIUM))
{
if (restaurant.getBudget() >= 800)
{
restaurant.setBudget(restaurant.getBudget() - 800);
if (i == 0)
System.out.println("The " + employees.get(a).job + " experience level is increased");
else
System.out.println(" and the " + employees.get(a).job + " experience level is increased");
}
else
{
if (i == 0)
System.out.println("The " + employees.get(a).job + " experience level is failed to increase");
else
System.out.println(" and the " + employees.get(a).job + " experience level is failed to increase");
}
}
else if (employees.get(a).experience.equals(ExperienceLevel.HIGH))
{
if (i == 0)
System.out.println("The " + employees.get(a).job + " experience level is increased");
else
System.out.println(" and the " + employees.get(a).job + " experience level is increased");
}
}
}
}
}
}

但是当我在 JUnit 测试方法中将预算设置为 10000 时,我不能将其恢复为 8800。但我仍然得到10000。可能是调用increaseExperience方法失败。

如何解决?

谢谢。

最佳答案

这就是问题,就在你的 increaseExperience 的开头。方法:

restaurant = new Restaurant();

您在该方法中使用的餐厅与测试中的餐厅完全分开。

也许您应该将餐厅作为另一个参数传递?或者员工可能应该知道他们正在工作的餐厅?

(也完全不清楚为什么您在没有任何有用信息的情况下在 increaseExperience 上调用 Employee - 听起来它应该是 Employee 上的实例方法没有您传入员工,或者它应该是 Restaurant 上的实例方法,此时您应该删除 restuarant 变量,或者它应该是 Employee 上的静态方法,因为它作用于许多员工,而不仅仅是一个......从根本上来说,我认为你需要重新审视你的设计。)

关于java - 从 JUnit 测试用例调用方法失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27100440/

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