简体   繁体   English

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

[英]Failure on calling a method from JUnit Test Case

I have a question: 我有个问题:

I have such a method in my JUnit Test case: 我的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());
}

And increaseExperience method in Employee class is: Employee类中的gainExperience方法为:

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");
                    }
                }
            }
        }
    }
}

But when I set the budget as 10000 in my JUnit Test method, I cannot I have to get it as 8800 back. 但是,当我在JUnit Test方法中将预算设置为10000时,我不必将其取回8800。 But still I get 10000. Probably there is a failure in calling increaseExperience method. 但是我仍然得到10000。可能在调用gainExperience方法时失败。

How can I solve it? 我该如何解决?

Thanks. 谢谢。

This is the problem, right at the start of your increaseExperience method: 这个问题就在您的increaseExperience方法开始时:

restaurant = new Restaurant();

The restaurant you're working with in the method is entirely separate from the restaurant in the test. 使用该方法的餐厅与测试中的餐厅完全分开。

Perhaps you should be passing in the restaurant as another parameter? 也许您应该将饭店作为另一个参数传递? Or possibly the employee should know the restaurant they're working for? 也许员工应该知道他们要去的餐厅?

(It's also not at all clear why you're calling increaseExperience on an Employee with no useful information - it sounds like either it should be an instance method on Employee without you passing in the employees, or it should be an instance method on Restaurant , at which point you should remove the restuarant variable, or it should be a static method on Employee as it acts on many employees, not just one... Fundamentally, I think you need to revisit your design.) (它也并不清楚为什么你调用increaseExperience上的Employee没有有用的信息-这听起来像它要么应该是一个实例方法, Employee 没有你的员工路过,还是应该对实例方法Restaurant ,在这一点上,您应该删除restuarant变量,或者对Employee来说,它应该是一个静态方法,因为它作用于许多员工,而不仅仅是一个……。从根本上讲,我认为您需要重新设计。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM