いつもお世話になっております
シンプルに説明すると、
1)時給(HourlyPaid)
2)歩合(SalesCommisioned)
3)プロの歩合(Executive)
の3つに従業員(Employee)を分けて、それぞれの月収or年収を算出するプログラムです
コレ自体の処理はできたのですが、機能をカバーする単体テストを作成したいと考えていて、単体テストの具体的な方法がわからず、困っています
単体テストを行うのはこれが初めてで、具体的な書き方がよくわかりません
ご助力いただけると幸いです
Employee class Name of employee
HourlyPaid class Rate of pay
Total weekly hours worked
SalesCommissioned class Percentage commission on total sales
Total sales for month
Executive class Incremental point on annual salary scale
The methods used in each class may be summarized as follows:
Employee class getName
computePay
HourlyPaid class getRate
getHours
computePay
SalesCommissioned class getPercentage
getSales
computePay
Executive class getIncrement
computePay
ファイル構造としては、 Employee がスーパークラスでHourlyPaidとSalesCommisioned がEmployeeのサブクラスです Executive はSalesCommisionedのサブクラスになります
Employee
1 2public abstract class Employee { 3 4 private String name; 5 6 public Employee(String name) { 7 super(); 8 this.name = name; 9 } 10 11 public String getName() { 12 return name; 13 } 14 15 public void setName(String name) { 16 this.name = name; 17 } 18 19 @Override 20 public String toString() { 21 return "Employee [name=" + name + "]"; 22 } 23} 24
HourlyPaid
1 2public class HourlyPaid extends Employee{ 3 4 public double wage;//wage per hour 5 public double hours;//total hours in a week 6 7 public HourlyPaid(String name, double wage, double hours) { 8 super(name); 9 this.wage = wage; 10 this.hours = hours; 11 } 12 13 public double getWage() { 14 return wage; 15 } 16 17 public void setWage(double wage) { 18 this.wage = wage; 19 } 20 21 public double getHours() { 22 return hours; 23 } 24 25 public void setHours(double hours) { 26 this.hours = hours; 27 } 28 29 public double earnings() { 30 if (getHours() <= 40) {//Not Overworked 31 return getHours()*getWage(); 32 } 33 //If it is more than 40 hours per week in Japan, hourly paid must times "1.25" 34 else return getWage()*40 + getWage()*1.25*(getHours()-40); 35 } 36 37 @Override 38 public String toString() { 39 return "HourlyPaid [wage=" + wage + ", hours=" + hours + "]"; 40 } 41} 42
SalesCommisioned
1 2public class SalesCommisioned extends Employee{ 3 4 private double commissionRate; // commission percentage 5 private double monthSales; // gross monthly sales 6 7 public SalesCommisioned(String name, double commissionRate, double monthSales) { 8 super(name); 9 this.commissionRate = commissionRate; 10 this.monthSales = monthSales; 11 } 12 13 public double getCommissionRate() { 14 return commissionRate; 15 } 16 17 public void setCommissionRate(double commissionRate) { 18 this.commissionRate = commissionRate; 19 } 20 21 public double getMonthSales() { 22 return monthSales; 23 } 24 25 public void setMonthSales(double monthSales) { 26 this.monthSales = monthSales; 27 } 28 29 public double earnings() { 30 return (1+getCommissionRate()/100) * getMonthSales(); 31 } 32 33 @Override 34 public String toString() { 35 return "SalesCommisioned [commissionRate=" + commissionRate + ", monthSales=" + monthSales + "]"; 36 } 37}
Executive
1 2public class Executive extends SalesCommisioned{ 3 4 private double incrementPercentage; 5 6 public Executive(String name, double commissionRate, double monthSales, double incrementPercentage) { 7 super(name, commissionRate, monthSales); 8 this.incrementPercentage = incrementPercentage; 9 } 10 11 public double getIncrement() { 12 return incrementPercentage; 13 } 14 15 public void setIncrement(double incrementPercentage) { 16 this.incrementPercentage = incrementPercentage; 17 } 18 19 public double annualSalary() { 20 return super.earnings()*12; 21 } 22 23 public double earningsAnnual() { 24 return (1+incrementPercentage/100) * annualSalary(); 25 } 26 27 @Override 28 public String toString() { 29 return "Executive [increment=" + incrementPercentage + "]"; 30 } 31}
TestEmployee
1import java.util.ArrayList; 2import java.util.List; 3 4public class TestEmployee { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 10 List<Object> emps= new ArrayList<Object>(); 11 12 int i = 0; 13 14 HourlyPaid hourlyPaid = new HourlyPaid( "Anne Bronte", 7.5, 45); 15 SalesCommisioned salesCommisioned = new SalesCommisioned("Emily Bronte", 30, 90); 16 Executive exective = new Executive("Charlotte Bronte", 12, 123, 30); 17 18 emps.add(i, hourlyPaid); 19 i++; 20 emps.add(i, salesCommisioned); 21 i++; 22 emps.add(i, exective); 23 i++; 24 25 for ( Object currentEmp : emps ) { 26 27 System.out.println( currentEmp ); 28 29 if( currentEmp instanceof HourlyPaid ) { 30 31 HourlyPaid employee = ( HourlyPaid ) currentEmp; 32 System.out.println("HourlyPaid Employee : " + employee.getName()); 33 System.out.println("Hourly Wage : " + employee.getWage()); 34 System.out.println("Earned(A Month) : " + employee.earnings()); 35 36 System.out.println(); 37 38 39 }else if ( currentEmp instanceof SalesCommisioned ) { 40 41 SalesCommisioned employee = ( SalesCommisioned ) currentEmp; 42 System.out.println("SalesCommisioned Employee : " + employee.getName()); 43 System.out.println("Commisioned Rate : " + employee.getCommissionRate() + "%"); 44 System.out.println("MonthSales : " + employee.getMonthSales()); 45 System.out.println("Earned(A Month) : " + employee.earnings()); 46 47 System.out.println(); 48 49 } 50 51 if ( currentEmp instanceof Executive ){ 52 53 Executive employee = ( Executive ) currentEmp; 54 55 System.out.println("AnnualSalay is " + employee.annualSalary() + " with IncrementRate " + employee.getIncrement() + "%"); 56 System.out.println("Earned(A year) : " + employee.earningsAnnual()); 57 58 System.out.println(); 59 } 60 } 61 } 62}
ここまでの実行結果
Console
1HourlyPaid [wage=7.5, hours=45.0] 2HourlyPaid Employee : Anne Bronte 3Hourly Wage : 7.5 4Earned(A Month) : 346.875 5 6SalesCommisioned [commissionRate=30.0, monthSales=90.0] 7SalesCommisioned Employee : Emily Bronte 8Commisioned Rate : 30.0% 9MonthSales : 90.0 10Earned(A Month) : 117.0 11 12Executive [increment=30.0] 13SalesCommisioned Employee : Charlotte Bronte 14Commisioned Rate : 12.0% 15MonthSales : 123.0 16Earned(A Month) : 137.76000000000002 17 18AnnualSalay is 1653.1200000000003 with IncrementRate 30.0% 19Earned(A year) : 2149.0560000000005 20
単体テストのファイル
UnitTest
1import java.io.ByteArrayOutputStream; 2import java.io.PrintStream; 3import java.util.ArrayList; 4import java.util.List; 5 6import org.junit.After; 7import org.junit.Before; 8import org.junit.Test; 9 10class UnitTest { 11 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); 12 @Before 13 public void setUpStreams() { 14 System.setOut(new PrintStream(outContent)); 15 } 16 17 @After 18 public void cleanUpStreams() { 19 System.setOut(System.out); 20 } 21 @Test 22 public void oilingTest01() { 23 24 List<Object> emps= new ArrayList<Object>(); 25 26 int i = 0; 27 28 HourlyPaid hourlyPaid = new HourlyPaid( "Anne Bronte", 7.5, 45); 29 SalesCommisioned salesCommisioned = new SalesCommisioned("Emily Bronte", 30, 90); 30 Executive exective = new Executive("Charlotte Bronte", 12, 123, 30); 31 32 emps.add(i, hourlyPaid); 33 i++; 34 emps.add(i, salesCommisioned); 35 i++; 36 emps.add(i, exective); 37 i++; 38 39 for ( Object currentEmp : emps ) { 40 41 System.out.println( currentEmp ); 42 43 if( currentEmp instanceof HourlyPaid ) { 44 45 HourlyPaid employee = ( HourlyPaid ) currentEmp; 46 System.out.println("HourlyPaid Employee : " + employee.getName()); 47 System.out.println("Hourly Wage : " + employee.getWage()); 48 System.out.println("Earned(A Month) : " + employee.earnings()); 49 50 System.out.println(); 51 52 53 }else if ( currentEmp instanceof SalesCommisioned ) { 54 55 SalesCommisioned employee = ( SalesCommisioned ) currentEmp; 56 System.out.println("SalesCommisioned Employee : " + employee.getName()); 57 System.out.println("Commisioned Rate : " + employee.getCommissionRate() + "%"); 58 System.out.println("MonthSales : " + employee.getMonthSales()); 59 System.out.println("Earned(A Month) : " + employee.earnings()); 60 61 System.out.println(); 62 63 } 64 65 if ( currentEmp instanceof Executive ){ 66 67 Executive employee = ( Executive ) currentEmp; 68 69 System.out.println("AnnualSalay is " + employee.annualSalary() + " with IncrementRate " + employee.getIncrement() + "%"); 70 System.out.println("Earned(A year) : " + employee.earningsAnnual()); 71 72 System.out.println(); 73 } 74 } 75 } 76 77 /* 78 //Exective Class 79 @Test 80 void getIncrement() { 81 fail("Not yet implemented"); 82 } 83 /* 84 @Test 85 void earningsAnnual(){ 86 fail("Not yet implemented"); 87 } 88 @Test 89 void annualSalary() { 90 fail("Not yet implemented"); 91 } 92 */ 93 94 /* 95 @Test 96 void earings() { 97 fail("Not yet implemented"); 98 } 99 100 @Test 101 void getName() { 102 fail("Not yet implemented"); 103 } 104 @Test 105 void getWage() { 106 fail("Not yet implemented"); 107 } 108 @Test 109 void getCommisionRate() { 110 fail("Not yet implemented"); 111 } 112 */ 113} 114
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。