2014-04-14 7 views
0

교수가 지정한 과제는 내가 작성한 클래스가 Javadoc과 일치해야하므로 JUnit 테스트에서 아무 것도 편집하지 못하게합니다. 그래서 나는 오류와 3 가지 실패를 제외하고는 모든 것이 잘 작동한다. 내가 뭘하려고해도 나는 이것을 고칠 수 없다. 오류를 수정하고 JUnit 테스트를 변경하지 않고 모든 테스트를 통과 시키려면 어떻게해야합니까? 오류 만 발견 할 수는 없습니다. 기본적으로 JUnit을 변경하지 않고 JUnits 및 오류에서 오류를 제거하는 방법은 일반적으로 어떻게됩니까? 여기 내 회사 클래스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; 
} 





} 
+1

그리고 어떤 오류가 발생합니까? 어떤 검사가 실패합니까? 그것은 도움이 될 것입니다. –

+0

또한'Employee' 클래스를 게시하십시오. –

답변

1

봐입니다. 그들이 당신의 프로그램이 기대하는 것을보십시오. 그런 다음 프로그램이 무엇을 생산하는지보십시오. 예를 들어,이 테스트 중 하나입니다 : 테스트를 의미

String result; 
//add another designer - No problem, should return null 
result = company.addEmployee("Bobby", "Match", "Design"); 
assertEquals(null, result); 

이 성공하면 null을 반환 addEmployee()을 기대하고있다. 그게 당신의 addEmployee() 방법이 무엇입니까?

이제 모든 테스트를 수행하면 모든 오류를 발견하게됩니다.

또한 jUnit의 출력을 살펴보십시오. assert이 실패하면 왜 좋은지 알 수 있습니다. 그것은 expected xxxxx to be yyyyy but it was zzzzz instead과 같은 것을 말할 것입니다.

+0

"예상되는 xxxxx는 yyyyy가 될 것이지만 대신 zzzzz"이라고 말하면 코드에서 찾을 수없는 것 같습니다. – user3530205

+0

늦게 회신 해 주셔서 죄송합니다. 여기 직원 클래스가 있습니다. – user3530205

+0

그리고 내가 말한 정확한 내용은 아니지만 오류 메시지는 어디에 있습니까? – user3530205

관련 문제