【问题标题】:Java: Unit Testing, JUnit, ErrorsJava:单元测试、JUnit、错误
【发布时间】: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 项目中?
  • 是的...我在另一个项目中编写了测试。我想这意味着我只需要上床睡觉。谢谢。除此之外看起来还可以吗?

标签: java eclipse junit junit4


【解决方案1】:

根据我们的评论讨论,您似乎在另一个 Eclipse 项目中编写了单元测试。通常测试位于同一个项目中,因此它们受益于将所有项目类都放在类路径中。

如果您想使用一个单独的项目,您需要edit the test project properties 告诉 Eclipse 您依赖于您的主项目。

【讨论】:

    【解决方案2】:

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

    这个错误意味着 Eclipse 找不到类 Developer 的代码。此问题与 JUnit 或单元测试无关。

    确保您的构建路径包含必要的 JAR 和项目。

    另外,在测试结束时删除fail(),因为您现在已经实现了测试。

    文档

    更好的记录方法是使用变量:

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

    变量解释了值的含义。 “2100”没有任何意义。 “预期”传达:这是预期的结果。

    您可以使用expectedDollars 表示“此值以美元为单位”。

    如果您只有一小段代码,请不要记录它们。人们可以阅读代码以了解发生了什么。但是您可以通过添加有用名称的方法来帮助他们:

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

    或者,如果您更喜欢简单的 DSL:

    Manager managerB = createManagerFor(
        new QATester(),
        new Developer()
    );
    Manager managerA = createManagerFor( managerB );
    

    【讨论】:

    • 非常有帮助。谢谢。
    【解决方案3】:

    应该删除测试用例末尾的fail 调用,无论上面行的断言结果如何,它都会始终使测试失败。

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 2023-03-15
      • 2011-06-18
      • 2021-08-08
      • 2012-01-04
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多