【发布时间】:2015-04-25 22:00:03
【问题描述】:
这是一个小型 Java 应用程序,旨在通过将支付给该部门所有员工的金额相加来计算一个部门的每月总支出。部门员工按层次结构排列,此应用试图在安排对象时考虑到这一层次。
我是 JUnit 和单元测试的新手。我正在尝试进行测试,将我的实际总数与预期总数进行比较。但是,我遇到了一些错误(在下面的源代码中有详细说明)。
注意我正在使用:Windows 8.1、Java SE 8、eclipse Luna、JUnit 4。
我有 1 个主要问题和 1 个附带问题:
您能否帮助我了解导致这些错误的原因以及我可以采取哪些措施来纠正这些错误?
我正在尝试通过从“cmets”转向更专业的东西来改进我的文档风格。可以改进此文档吗?
感谢观看。
单元测试
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?
}
}
应用
//员工
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;
}
}
// 开发者
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;
}
}
// 经理
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;
}
}
【问题讨论】:
-
当您尝试编译测试时,您的
expenseApp包不在您的类路径中。这会导致导入失败,在整个测试过程中触发编译错误。您的单元测试代码是否在不同的 Eclipse 项目中? -
是的...我在另一个项目中编写了测试。我想这意味着我只需要上床睡觉。谢谢。除此之外看起来还可以吗?