所以我教授给出的作业不允许我在JUnit测试中编辑任何内容,因为我们编写的类必须与她的Javadoc匹配。所以一切正常,除了我收到错误和3次失败。无论我尝试什么,我似乎无法解决这些问题。如何在不更改JUnit测试的情况下修复错误并使所有测试通过,我无法发现错误。基本上一般来说,如何在不更改JUnit的情况下摆脱JUnit中的错误和失败。 这是我的公司类
/**
* @author Amin Oskoui
* This is the company class aka the data manager which contains the majority of the methods and holds the arraylist.
*/
import javax.swing.*;
import java.util.*;
public class Company {
private static final long serialVersionUID = 1L;//ensuring that the class corresponds with a serialized object
Employee a;
private String companyName;//name of company
final int maxCompanies = 2, maxEmployees = 7, maxSales = 1, maxDesign = 2, maxManufacturing = 4;
private static int numberOfCompanies;//the number of companies
private int numEmployees;//the number of employees
public int numDesign;//the number of employees in design
private int numManufacturing;// the number of employees in manufacturing
private int numSales;//the number of employees in sales
private ArrayList<Employee> employeeList;
/**
*
* @param cn parameter for cn
*/
public Company(String cn){
numEmployees = 0;
numSales = 0;
numDesign = 0;
numManufacturing = 0;
companyName = cn;
numberOfCompanies++;
employeeList = new ArrayList<Employee>();
}
/**
*
* @param employeeCount parameter for employeeCount
* @param numDesign parameter for numDesign
* @param numSales parameter for numSales
* @param numManufacturing parameter for numManufacturing
* @param companyName parameter for companyName
*/
public Company(int employeeCount, int numDesign, int numSales, int numManufacturing, int numOfCompanies) {
this.numEmployees = employeeCount;
this.numDesign = numDesign;
this.numSales = numSales;
this.numManufacturing = numManufacturing;
this.companyName = companyName;
numberOfCompanies++;
employeeList = new ArrayList<Employee>();
}
/**
*
* @param fName parameter for fName
* @param lName parameter for lName
* @param parameter for p
* @return
*/
public String addEmployee(String fName, String lName, String p) {
/**
* @return returns a string with an error message
*/
if (numEmployees >= maxEmployees) {
return "There is already 7 employees\nEmployee not added";
}
switch (p) {//switch statement for each case
case "Sales":
//if there's less than 1 salesman, add them to the list
if (numSales < maxSales) {
Employee employee = new Employee(fName, lName, p);
employeeList.add(employee);
numSales++;
numEmployees++;
return "Salesperson added successfully!";
}
else {
/**
* @return returns a string with an error message
*/
return "There is already a sales person\nEmployee not added";
}
case "Design":
if (numDesign < maxDesign) {
Employee employee = new Employee(fName, lName, p);
employeeList.add(employee);
numDesign++;
numEmployees++;
return "Designer added successfully!";
}
else {
/**
* @return returns a string with an error message
*/
return "There are already two design persons\nEmployee not added";
}
case "Manufacturing":
if (numManufacturing < maxManufacturing){
Employee employee = new Employee(fName, lName, p);
employeeList.add(employee);
numManufacturing++;
numEmployees++;
return "Manufacturer added successfully!";
}
else {
/**
* @return returns a string with an error message
*/
return "There are already four manufacturing persons\nEmployee not added";
}
}
/**
* @return return statement just to make sure the program operates properly.
*/
return "This should never appear";
}
public static int getNumCompanies(){//return the number of companies
return numberOfCompanies;
}
public int getNumEmployees(){//get the number of employees
return numEmployees;
}
public String printCompany(){//print the company with all of the positions
String companyPrint = companyName + "\n";
return companyName;
}
public String employeeListString() {
String s;
s = companyName + "\n";
for (Employee e : employeeList) {
s += e + "\n";
}
return s;
}
/**
* @param s passes the String s
*/
public void setCompanyName(String s) {
companyName = s;
}
public void clearEmployees() {
numEmployees = numDesign = numManufacturing = numSales = 0;
employeeList.clear();
}
@Override
public String toString() {//converts everything to a string
return "Company [position=" + ", companyName=" + companyName
+ ", employees=" + employeeList + ", numEmployees=" + numEmployees
+ ", numDesign=" + numDesign + ", numManufacturing="
+ numManufacturing + ", numSales=" + numSales + "]";
}
}
这是JUnit的测试。
import static org.junit.Assert.*;
import java.util.Scanner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* This is a JUnit test program to test the Company.
* The following classes must be defined and implemented:
* Position - enumeration
* Employee - data element
* Company - data manager
* @author Professor Myers changed by Prof. Justh
*
*/
public class CompanyTester {
/** A Company object reference used for testing */
Company company, studentCompany;
@Before
/** This method is run before each individual test
* Creates an object of Company and adds three
* Employees to the Company
*/
public void setUp() throws Exception {
company = new Company("New Source");
company.addEmployee("John", "Smith","Manufacturing");
company.addEmployee("Bob", "Brown", "Manufacturing");
company.addEmployee("Harold", "Jones", "Sales");
company.addEmployee("Betty","Boop", "Design");
//STUDENT: Create your own instance of company (studentCompany) and add employees.
//You will use this studentCompany instance in the STUDENT test methods
}
@After
/** This method is run after each individual test
* It sets the Company reference to null so the garbage
* collector can reclaim the memory used for the
* Company object
* @throws Exception
*/
public void tearDown() throws Exception {
company = null;
}
@Test
/**
* Test to
* 1. check if the number of employees is originally 4
* 2. Add another employee, and check if number of employees is 5
*
*/
public void testGetNumEmployees() {
assertEquals(4,company.getNumEmployees());
company.addEmployee("George", "Jones", "Design");
assertEquals(5, company.getNumEmployees());
company.addEmployee("Susie", "Smith", "Manufacturing");
assertEquals(6, company.getNumEmployees());
company.addEmployee("Susie", "Smiley", "Manufacturing");
assertEquals(7, company.getNumEmployees());
}
@Test
/**
* Use the studentCompany instance
* Test to
* 1. check the original number of employees
* 2. Add another employee, and check if number of employees has increased by 1
*
*/
public void testGetNumEmployeesSTUDENT() {
assertEquals(4,company.getNumEmployees());
company.addEmployee("John", "Mayhew", "Design");
assertEquals(5, company.getNumEmployees());
company.addEmployee("Sally", "Sams", "Manufacturing");
assertEquals(6, company.getNumEmployees());
company.addEmployee("Max", "Schmidt", "Manufacturing");
assertEquals(7, company.getNumEmployees()); }
@Test
/**
* Test to
* 1. add 3 new Employees as a manufacturing person
* check if recognizes there are already 4 manufacturing persons
* 2. add a new employee as a sales person
* check if recognizes there is already a a sales person
* 3. add 2 new employee as a design person
* check if recognizes there is already 2 design persons
*/
public void testAddEmployee() {
String result;
//add another designer - No problem, should return null
result = company.addEmployee("Bobby", "Match", "Design");
assertEquals(null, result);
//add another designer - already 2 designers - return error message
result = company.addEmployee("Albert","Pine", "Design");
assertEquals("There are already two design persons\nEmployee not added", result);
//add another sales person - already 1 sales person - return error message
result = company.addEmployee("Susie", "Smith", "Sales");
assertEquals("There is already a sales person\nEmployee not added", result);
//add another manufacturer - No problem, should return null
result = company.addEmployee("Benedict", "Cumberbatch", "Manufacturing");
assertEquals(null, result);
//add another manufacturer - No problem, should return null
result = company.addEmployee("Martin", "Freeman", "Manufacturing");
assertEquals(null, result);
//add another manufacturer - already 4 manufacturers - return error message
result = company.addEmployee("Andrew", "Scott", "Manufacturing");
assertEquals("There are already four manufacturing persons\nEmployee not added", result);
}
@Test
/**
* Test to
* 1. add a new Employees as a manufacturing person
* check if recognizes there are already 4 manufacturing persons
* 2. add a new employees as a sales person
* check if recognizes there is already a a sales person
* 3. add new employees as a design person
* check if recognizes there are already 2 design persons
*/
public void testAddEmployeeSTUDENT() {
fail("Test not yet implemented");
}
@Test
/**
* Test to:
* 1. Check if the company name is on the first line
* 2. Check if John is on the second line
* 3. Check if Bob is on the third line
* 4. Check if Harold is on the fourth line
* 5. Check if Betty is on the fifth line
*/
public void testPrintCompany() {
String result = company.printCompany();
Scanner company = new Scanner(result);
assertEquals("New Source",company.nextLine());
//extract three Employees
assertEquals("John", company.next());
company.nextLine(); //Smith Position design (rest of line)
assertEquals("Bob", company.next());
company.nextLine(); //Brown Position manufacturing (rest of line)
assertEquals("Harold",company.next()); //Harold
company.nextLine(); //Jones Position Sales (rest of line);
assertEquals("Betty",company.next());
}
@Test
/**
* Test to:
* 1. Check if the company name is on the first line
* 2. Check if other employees are in order as entered
*/
public void testPrintCompanySTUDENT() {
fail("Test not yet implemented");
}
@Test
public void testMoreThan1company()
{
//created company and studentCompany instances
assertEquals(2, Company.getNumCompanies());
//create another company instance
Company company2 = new Company("New Company");
assertEquals(3, Company.getNumCompanies());
}
}
这是员工类
/**
* @author This is the employee class which is also the data element in charge of holding the information for the employee entered by the user.
*/
import javax.swing.JOptionPane;
public class Employee {
private String fName;
private String lName;
private String p;
public Employee(String fName, String lName, String p) {
this.fName = fName;
this.lName = lName;
this.p = p;
}
public String getFName(){
return fName;
}
public String getLName(){
return lName;
}
public String getP(){
return p;
}
public String toString(){
return fName + " " + lName + " " + "Position: " + p;
}
}
答案 0 :(得分:1)
仔细阅读测试。看看他们期望你的程序产生什么。然后看看你的程序正在生成什么。例如,这来自其中一个测试:
String result;
//add another designer - No problem, should return null
result = company.addEmployee("Bobby", "Match", "Design");
assertEquals(null, result);
这意味着测试期望addEmployee()
在成功时返回null
。那是你addEmployee()
方法的作用吗?
现在为所有测试做到这一点,你就找到了所有错误。
另外,查看jUnit的输出。当assert
失败时,它会为您提供一个非常好的主意。它会说expected xxxxx to be yyyyy but it was zzzzz instead
。