Java:单元测试,JUnit,错误
Java: Unit Testing, JUnit, Errors
这是一个小型 Java 应用程序,旨在通过将支付给该部门所有员工的金额相加来计算该部门的每月总开支。部门员工按层次结构排列,此应用在安排对象时力求考虑到这一点。
我是 JUnit 和单元测试的新手。我正在尝试 运行 一项测试,将我的实际总数与预期总数进行比较。但是,我遇到了一些错误(在下面的源代码中有详细说明)。
注意我正在使用:Windows 8.1,Java SE 8,eclipse Luna,JUnit 4。
我有 1 个主要问题和 1 个辅助问题:
您能帮我了解导致这些错误的原因以及我可以做些什么来纠正它们吗?
我正在尝试通过从 'comments' 转向更专业的方式来改进我的文档风格。可以改进此文档吗?
感谢观看。
单元测试
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 00.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 0.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 00.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 0.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;
}
}
应该删除测试用例末尾的 fail
调用,无论上面一行的断言结果如何,它总是会导致测试失败。
import expenseApp.Developer; // The import expenseApp cannot be resolved
此错误意味着 Eclipse 无法找到 class Developer
的代码。此问题与 JUnit 或单元测试无关。
确保您的构建路径包含必要的 JAR 和项目。
此外,删除测试末尾的 fail()
,因为您现在已经实施了测试。
documentation
更好的记录方式是使用变量:
double expected = 2100.0;
double maxDelta = 1e-6;
assertEquals(expected, managerA.getTotalExpenses(), maxDelta);
变量解释了值的含义。 “2100”没有任何意义。 "expected"传达:这是预期的结果。
你可以用expectedDollars
来表示"this value is in dollars"。
如果您只有一小段代码,请不要记录它们。人们可以阅读代码以了解发生了什么。但是您可以通过添加具有有用名称的方法来帮助他们:
Manager managerB = managerWithADevAndTester();
Manager managerA = createManagerFor( managerB );
或者,如果您更喜欢简单的 DSL:
Manager managerB = createManagerFor(
new QATester(),
new Developer()
);
Manager managerA = createManagerFor( managerB );
根据我们的评论讨论,您似乎在另一个 Eclipse 项目中编写了单元测试。通常测试位于同一个项目中,因此它们受益于将所有项目 类 放在类路径中。
如果您想使用单独的项目,您需要edit the test project properties 告诉 Eclipse 您依赖于主项目。
这是一个小型 Java 应用程序,旨在通过将支付给该部门所有员工的金额相加来计算该部门的每月总开支。部门员工按层次结构排列,此应用在安排对象时力求考虑到这一点。
我是 JUnit 和单元测试的新手。我正在尝试 运行 一项测试,将我的实际总数与预期总数进行比较。但是,我遇到了一些错误(在下面的源代码中有详细说明)。
注意我正在使用:Windows 8.1,Java SE 8,eclipse Luna,JUnit 4。
我有 1 个主要问题和 1 个辅助问题:
您能帮我了解导致这些错误的原因以及我可以做些什么来纠正它们吗?
我正在尝试通过从 'comments' 转向更专业的方式来改进我的文档风格。可以改进此文档吗?
感谢观看。
单元测试
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 00.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 0.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 00.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 0.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;
}
}
应该删除测试用例末尾的 fail
调用,无论上面一行的断言结果如何,它总是会导致测试失败。
import expenseApp.Developer; // The import expenseApp cannot be resolved
此错误意味着 Eclipse 无法找到 class Developer
的代码。此问题与 JUnit 或单元测试无关。
确保您的构建路径包含必要的 JAR 和项目。
此外,删除测试末尾的 fail()
,因为您现在已经实施了测试。
documentation
更好的记录方式是使用变量:
double expected = 2100.0;
double maxDelta = 1e-6;
assertEquals(expected, managerA.getTotalExpenses(), maxDelta);
变量解释了值的含义。 “2100”没有任何意义。 "expected"传达:这是预期的结果。
你可以用expectedDollars
来表示"this value is in dollars"。
如果您只有一小段代码,请不要记录它们。人们可以阅读代码以了解发生了什么。但是您可以通过添加具有有用名称的方法来帮助他们:
Manager managerB = managerWithADevAndTester();
Manager managerA = createManagerFor( managerB );
或者,如果您更喜欢简单的 DSL:
Manager managerB = createManagerFor(
new QATester(),
new Developer()
);
Manager managerA = createManagerFor( managerB );
根据我们的评论讨论,您似乎在另一个 Eclipse 项目中编写了单元测试。通常测试位于同一个项目中,因此它们受益于将所有项目 类 放在类路径中。
如果您想使用单独的项目,您需要edit the test project properties 告诉 Eclipse 您依赖于主项目。