Failure on calling a method from JUnit Test Case

I have a question:

I have such a method in my JUnit Test case:

@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:

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. But still I get 10000. Probably there is a failure in calling increaseExperience method.

How can I solve it?

Thanks.

Jon Skeet
people
quotationmark

This is the problem, right at the start of your increaseExperience method:

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.)

people

See more on this question at Stackoverflow