So I have 3 packages, Implementation, interfaces and test. I want to write the functionality into EmployeeImp so my unit test passes without error when I run it on TestEmployeeImp. However I'm not sure how getEmployeeCount is written as it fails in the unit test. I tried to solve it by creating the int count but it doesn't work. I know I need to use the array list to count the number of employees but I cannot come up with a solution and I can't find any samples of code that are like my unit test. If anyone can help it would be very appreciated.
//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());
}
}
CodePudding user response:
I can't see any code which initialize or increment int count
variable. But as you said, you don't need count
variable and just use size()
method in employees
List
@Override
public int getEmployeeCount() {
return this.employees.size();
}
CodePudding user response:
In @BeforeEach you're test by creating 4 employees.
- Your hire method does 'employees.add(p);' , so it expands your list.
- Your fire method does not do anything, just returning false.
Yet you expect in test testFire
and testGetEmployeeCount
that the number of employees has decreased. That does not happen and will fail.
You need the following fix: IMPORTANT - Implement an equals and hash code on your PersonImpl class (so you can compare equal objects content instead of object-hash value). You can use guava or apache commmons or lombok or any other way to do that.
Then implement in 'fire' method:
@Override
public boolean fire(Person p) {
return employees.remove(p);
}
In this case I assume you will implement limitations in the 'hire' method on your class to have duplicate employees, so you need only to remove it once. If employees can be duplicate, then do to remove the employee including duplicates:
return employees.removeAll(Collections.singletonList(p));