我有一个问题:
我在JUnit Test案例中有这样的方法:
@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类中增加体验方法是:
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 Test方法中将预算设置为10000时,我无法将其作为8800返回。但是我仍然得到10000.可能在调用increaseExperience方法时失败了。
我该如何解决?
感谢。
答案 0 :(得分:1)
这是问题,就在increaseExperience
方法的开头:
restaurant = new Restaurant();
您在该方法中使用的餐厅与测试中的餐厅完全分开。
也许你应该在餐厅作为另一个参数传递?或者员工应该知道他们为之工作的餐厅?
(它也完全不清楚为什么你在increaseExperience
上调用Employee
而没有任何有用的信息 - 听起来它应该是{{1}上的实例方法{1}} 没有您传入员工,或者它应该是Employee
上的实例方法,此时您应该删除Restaurant
变量,或者它应该是restuarant
上的静态方法,因为它对许多员工起作用,而不仅仅是一个......从根本上说,我认为你需要重新审视你的设计。)