0

This is a small Java application that is designed to calculate a departments total monthly expenses by adding the amounts paid to all the employees in that department. The departments employees are arranged in a hierarchy, which this app seeks to take into account with how it arranges objects.

I'm new to JUnit and unit testing. I'm attempting to run a test comparing my actual total to an expected total. However, I'm getting a number of error(detailed in the source code below).

Note I'm using: Windows 8.1, Java SE 8, eclipse Luna, JUnit 4.

I have 1 main question, & 1 side questions:

  • Could you help me to understand what is causing these errors and what I can do to correct them?

  • I'm trying to improve my documentation style by moving from 'comments' to something a little more professional. Could this documentation be improved?

Thanks for taking a look.

Unit Test

package test;

import static org.junit.Assert.*;
import org.junit.Test;
import expenseApp.Developer; // The import expenseApp cannot be resolved
import expenseApp.Manager; // The import expenseApp cannot be resolved
import expenseApp.QATester; // The import expenseApp cannot be resolved

/**
 * TestClass evaluates a departments actual total expenses,
 * by comparing them to a projected total.
 * @author Reed
 */
public class TestClass {

    /**
     * testOne() compares a departments actual total expenses with a projected total of $2100.00.
     * This departments employees create the following hierarchy:
     * managerA <-- managerB <-- tester1 &  dev1.
     */
    @Test
        public void testOne() 
        {
            QATester tester1 = new QATester(); // Multiple markers at this line - QATester cannot be resolved to a type x2
            Developer dev1 = new Developer(); // Multiple markers at this line - Developer cannot be resolved to a type x2

            Manager managerB = new Manager(); // Multiple markers at this line - Manager cannot be resolved to a type x2
            managerB.add(tester1);
            managerB.add(dev1);

            Manager managerA = new Manager(); // Multiple markers at this line - Manager cannot be resolved to a type x2
            managerA.add(managerB);

            assertEquals(managerA.getTotalExpenses(), 2100.00, 0.00);

            fail("Not yet implemented"); // automatically generated, should I use this? 
        }

}

App

//Employee

package expenseApp;

/**
 * Employee is the abstract superclass of Manager, QATester, and Developer.
 * Employee declares public abstract double getExpenses().
 * @author Reed
 */
public abstract class Employee 
{
    /**
     * getExpenses() returns the monthly allocation amount of a Manager, Developer, or QATester object.
     * @return a double values representing what the specified Employee is paid each month.
     */
    public abstract double getExpenses();
}

// QATester

package expenseApp;

/**
 * QA Testers warrant a monthly allocation of $500.00, per QA Tester.
 * QATester extends Employee.
 * @author Reed
 */
public class QATester extends Employee
{
     /**
     * getExpenses() returns a QA Testers monthly allocation amount.
     * @return a double value of 500.00.
     */
    @Override
    public double getExpenses() 
    {
        return 500.00;
    }

}

// Developer

package expenseApp;

/**
 * Developers warrant a monthly allocation of $1000.00, per Developer.
 * Developer extends Employee.
 * @author Reed
 */
public class Developer extends Employee
{
    /**
     * getExpenses() returns a Developers monthly allocation amount.
     * @return a double value of 1000.00.
     */
    @Override
        public double getExpenses() 
        {
            return 1000.00;
        }
}

// Manager

package expenseApp;

import java.util.ArrayList;

/**
 * Managers warrant a monthly allocation of $300.00, per Manager.
 * 
 * A manager is at the top of a hierarchical relationship,
 * in which one manager oversees employees such as developers,
 * QA testers, & other managers. An employee's title is associated with
 * an amount that type of employee is paid monthly.
 * A compete hierarchy constitutes all the employees of a department. 
 * A departments expenses can be determined by adding the amounts
 * paid to the employees in a hierarchy.
 * 
 * Manager extends Employee.
 * 
 * @author Reed
 */
public class Manager extends Employee
{

    private ArrayList<Manager> managerList = new ArrayList<>();
    private ArrayList<Employee> employeeList = new ArrayList<>();

    /**
     * Add() adds employees to a list.
     * If the employee is a manager, managerList.
     * Else, the employee is a developer or QA tester, employeeList.
     * @param employee
     */
    public void add(Employee employee) 
    {
        if(employee instanceof Manager) 
        {
            managerList.add((Manager) employee);

        }
        else 
        {
            employeeList.add(employee);
        }
    }

    /**
     * getExpenses() returns a Mangers monthly allocation amount.
     * @return a double value of 300.00.
     */
    @Override
        public double getExpenses() 
        {
            return 300.00;
        }

    /**
     * getTotalExpenses() adds the values in managerList and employeeList,
     * calculating a departments total monthly expenses.
     * @return the value of totalExpenses.
     */
    public double getTotalExpenses() 
    {
        double totalExpenses = 0.00;

        for(Manager manager : managerList)
        {
            totalExpenses += manager.getTotalExpenses();
        }

        for(Employee employee : employeeList)
        {
            totalExpenses += employee.getExpenses();
        }

        return totalExpenses;
    }
}
Holloway
  • 6,412
  • 1
  • 26
  • 33
Reed Williams
  • 127
  • 1
  • 1
  • 12
  • 1
    Your `expenseApp` package is not on your classpath when you try to compile your tests. This causes the imports to fail, triggering compile errors throughout your test. Is your unit test code in a different Eclipse project? – Duncan Jones Feb 24 '15 at 10:33
  • yes...i wrote the test in a different project. I think that means I just need to go to bed. thank you. Look ok besides that? – Reed Williams Feb 24 '15 at 10:40

3 Answers3

1

import expenseApp.Developer; // The import expenseApp cannot be resolved

This error means that Eclipse can't find the code for the class Developer. This problem is not related to JUnit or unit tests.

Make sure your build path contains the necessary JARs and projects.

Also, remove the fail() at the end of the test since you have now implemented the test.

documentation

A better way to document is to use variables:

double expected = 2100.0;
double maxDelta = 1e-6;
assertEquals(expected, managerA.getTotalExpenses(), maxDelta);

The variables explain what a value means. "2100" means nothing. "expected" communicates: That's the expected result.

You could use expectedDollars to say "this value is in dollars".

If you have only short pieces of code, don't document them. People can read the code to understand what is going on. But you can help them by adding methods with useful names:

Manager managerB = managerWithADevAndTester();
Manager managerA = createManagerFor( managerB );

or, if you prefer simple DSLs:

Manager managerB = createManagerFor(
    new QATester(),
    new Developer()
);
Manager managerA = createManagerFor( managerB );
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

The fail call at the end of the test case should be removed, that will always fail the test regardless of the assertion result on the line above.

user11171
  • 3,821
  • 5
  • 26
  • 35
0

Based on our comment discussion, it seems you wrote your unit tests in another Eclipse project. Typically tests are located within the same project, thus they benefit from having all the project classes on the classpath.

If you'd like to use a separate project, you'll need to edit the test project properties to tell Eclipse that you have a dependency on your main project.

Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254