6月5日如何统计UnitTest中的员工人数

xqnpmsa8  于 2022-10-15  发布在  Eclipse
关注(0)|答案(2)|浏览(146)

所以我有3个包,实现,接口和测试。我希望将该功能写入EmployeeImp,以便在TestEmployeeImp上运行单元测试时不会出错。但是,我不确定getEmployeeCount是如何编写的,因为它在单元测试中失败了。我试图通过创建int count来解决它,但它不起作用。我知道我需要使用数组列表来计算员工数量,但我想不出解决方案,也找不到任何与我的单元测试类似的代码样本。如果有人能帮上忙,我将不胜感激。

//EmployeeImp

import java.util.ArrayList;
import java.util.List;

import interfaces.Employer;
import interfaces.Person;

public class EmployerImpl implements Employer {

    private String name;
    private List<Person> employees;
    private int count;

    public EmployerImpl(String n) {
        //gets name
        this.name = n;
        //Array List
        employees = new ArrayList<Person>();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void hire(Person p, String title, double salary) {
        p.setJob(null);
        employees.add(p);

    }

    @Override
    public List<Person> getEmployees() {
        //Returns Employees in a List
        return employees;
    }

    @Override
    public int getEmployeeCount() {
        return this.count;
        //Returns employees size

    }

    @Override
    public boolean fire(Person p) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public String getName() {
        //returns name
        return name;
    }

    @Override
    public boolean isEmployed(Person p) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Person getHighestPaid() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Person getLowestPaid() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public double getStaffCost() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getCountOf(String title) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<Person> getAll(String title) {
        // TODO Auto-generated method stub
        return null;
    }

}
//Employer.java
import java.util.List;

public interface Employer {

    void hire(Person p, String title, double salary);
    List<Person> getEmployees();
    int getEmployeeCount();
    boolean fire(Person p);
    String getName();
    boolean isEmployed(Person p);
    Person getHighestPaid();
    Person getLowestPaid();
    double getStaffCost();
    int getCountOf(String title);
    List<Person> getAll(String title);
}
//TestEmployeeImp

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import implementation.EmployerImpl;
import implementation.PersonImpl;
import interfaces.Employer;
import interfaces.Person;

class TestEmployerImpl {

    private Employer e;
    private Person highest;
    private Person lowest;

    @BeforeEach
    void setUp() throws Exception {
        e = new EmployerImpl("NCCO");
        lowest = new PersonImpl("John", 18);
        e.hire(lowest, "Lab Assistant", 20000);
        highest = new PersonImpl("Anu", 50);
        e.hire(highest, "Best Teacher", 80000);
        e.hire(new PersonImpl("Damien", 18), "Teacher", 41000);
        e.hire(new PersonImpl("Malachy", 45), "Teacher", 50000);

    }

    @Test
    void testGetEmployees() {
        List<Person> l = e.getEmployees();
        assertNotNull(l);
        assertEquals(4, l.size());
    }

    @Test
    void testGetEmployeeCount() {
        assertEquals(4, e.getEmployeeCount());
        Person p = new PersonImpl("Paul H", 50);
        e.hire(p, "teacher", 1000);
        assertEquals(5, e.getEmployeeCount());
        e.fire(p);
        assertEquals(4, e.getEmployeeCount());
    }

    @Test
    void testFire() {
        Person p = new PersonImpl("Damien", 18);
        boolean f= e.fire(p);
        assertTrue(f);
        assertEquals(3, e.getEmployeeCount());

        p = new PersonImpl("Danika", 23);
        f = e.fire(p);
        assertFalse(f);
    }

    @Test
    void testGetName() {
        assertEquals("NCCO", e.getName());
    }

    @Test
    void testIsEmployed() {
        Person p = new PersonImpl("Damien", 18);
        assertTrue(e.isEmployed(p));

        p = new PersonImpl("Danika", 23);
        assertFalse(e.isEmployed(p));
    }

    @Test
    public void testGetHighestPaid() {
        assertEquals(highest, e.getHighestPaid());
    }

    @Test
    void getLowestPaid() {
        assertEquals(lowest, e.getLowestPaid());
    }

    @Test
    void getStaffCost() {
        assertEquals(191000, e.getStaffCost());
    }

    @Test
    void testGetCountOf() {
        assertEquals(2, e.getCountOf("Teacher"));
        assertEquals(0, e.getCountOf("Awesome Teacher"));
    }

    @Test 
    void testGetAll(){
        assertEquals(2, e.getAll("Teacher").size());
        assertNotNull(e.getAll("Dean"));
        assertTrue(e.getAll("Dean").isEmpty());
    }

}
btqmn9zl

btqmn9zl1#

我看不到任何初始化或递增int count变量的代码。但正如您所说,您不需要count变量,只需使用employees列表中的size()方法即可

@Override
public int getEmployeeCount() {
    return this.employees.size();
}
brvekthn

brvekthn2#

在@BeForeEach中,您通过创建4名员工进行测试。

  • 您的雇用方法做了‘Employees.Add(P);’,因此它扩展了您的列表。
  • 您的Fire方法不做任何事情,只返回FALSE。

然而,在测试testFiretestGetEmployeeCount中,您预计员工数量已经减少。这种情况不会发生,也会失败。
您需要以下修复:重要-在PersonImpl类上实现一个等于和散列代码(这样您就可以比较相等的对象内容,而不是对象散列值)。您可以使用Guava或ApacheCommmons或Lombok鱼或任何其他方式来做到这一点。
然后在‘fire’方法中实现:

@Override
    public boolean fire(Person p) {
        return employees.remove(p);
    }

在这种情况下,我假设您将在您的类的‘hiit’方法中实现限制,以拥有重复的雇员,因此您只需要删除它一次。如果员工可以重复,则删除员工,包括重复的员工:

return employees.removeAll(Collections.singletonList(p));

相关问题