質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

Q&A

解決済

1回答

528閲覧

機能をカバーする単体テストの作成方法

Eston

総合スコア67

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

0グッド

2クリップ

投稿2019/10/20 14:38

いつもお世話になっております

下記の画像を参照した、練習問題を説いているのですが、
イメージ説明

シンプルに説明すると、
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 がスーパークラスで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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

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 @Override 25 public boolean equals(Object arg0) { 26 Employee other = (Employee) arg0; 27 return this.getName().equalsIgnoreCase(other.getName()); 28 } 29 30 31 32} 33

Exective

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 32 @Override 33 public boolean equals(Object arg0) { 34 Employee other = (Employee) arg0; 35 return this.getName().equalsIgnoreCase(other.getName()); 36 } 37 38 39} 40

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 38 @Override 39 public boolean equals(Object arg0) { 40 Employee other = (Employee) arg0; 41 return this.getName().equalsIgnoreCase(other.getName()); 42 } 43 44 45} 46

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 8 public HourlyPaid(String name, double wage, double hours) { 9 super(name); 10 this.wage = wage; 11 this.hours = hours; 12 } 13 14 15 public double getWage() { 16 return wage; 17 } 18 19 20 public void setWage(double wage) { 21 this.wage = wage; 22 } 23 24 25 public double getHours() { 26 return hours; 27 } 28 29 30 public void setHours(double hours) { 31 this.hours = hours; 32 } 33 34 35 public double earnings() { 36 if (getHours() <= 40) {//Not Overworked 37 return getHours()*getWage(); 38 } 39 //If it is more than 40 hours per week in Japan, hourly paid must times "1.25" 40 else return getWage()*40 + getWage()*1.25*(getHours()-40); 41 } 42 43 44 @Override 45 public String toString() { 46 return "HourlyPaid [wage=" + wage + ", hours=" + hours + "]"; 47 } 48 49 @Override 50 public boolean equals(Object arg0) { 51 Employee other = (Employee) arg0; 52 return this.getName().equalsIgnoreCase(other.getName()); 53 } 54 55 56} 57

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} 63

UnitTest

1import org.junit.Assert; 2import org.junit.Test; 3 4public class UnitTest { 5 6 @Test 7 public void testEquals() { 8 HourlyPaid hp = new HourlyPaid("TestHP", 10, 10); 9 SalesCommisioned sc = new SalesCommisioned("TestSC", 20, 20); 10 Executive ex = new Executive("TestEX", 30, 30, 30); 11 12 //HourlyPaid Test 13 hp.getName(); 14 hp.getWage(); 15 hp.getHours(); 16 hp.earnings(); 17 hp.toString(); 18 19 //SalesCommisioned Test 20 sc.getName(); 21 sc.getCommissionRate(); 22 sc.getMonthSales(); 23 sc.earnings(); 24 25 //Executive Test 26 ex.getName(); 27 ex.getIncrement(); 28 ex.annualSalary(); 29 ex.earningsAnnual(); 30 ex.toString(); 31 32 //Check 1st parameter is「Expected Value」, 2nd parameter is「Actual Value」 33 //So need to set same value and check 34 HourlyPaid hp2 = new HourlyPaid("TestHP", 10, 10); 35 SalesCommisioned sc2 = new SalesCommisioned("TestSC", 20, 20); 36 Executive ex2 = new Executive("TestEX", 30, 30, 30); 37 Assert.assertEquals(true, hp.equals(hp2)); 38 Assert.assertEquals(true, sc.equals(sc2)); 39 Assert.assertEquals(true, ex.equals(ex2)); 40 41 } 42 43} 44

投稿2019/11/05 22:19

編集2019/11/05 22:27
Eston

総合スコア67

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問