私の先生はクラスの JUnit テスト ケースを提供してくれました。OrderManager
私は自分のOrderManager
クラスを作成しましたが、私のコードは彼女が提供した 4 つのテストのうち 3 つに失敗しました。クラスのコード、提供された JUnit テスト、および JUnit テストが示すエラーを貼り付けます。
これが私の注文マネージャークラスです:
import java.text.DecimalFormat;
import java.util.ArrayList;
public class OrderManager implements OrderManagerInterface
{
private double order;
private int numberOfOrders;
DecimalFormat format = new DecimalFormat("##.00");
Sorting sortingClass = new Sorting();
//create an array list of type employee
ArrayList<Comparable> orderList = new ArrayList<Comparable>();
/**
* Initialize the numWorkers, numManager, numHourlyWorker, numThrowers to 0.
* @param
*/
public OrderManager()
{
}
public double addOrder(String toLn, String toFn, String toStr, String toC, String toSt, int toZ,
String fromLn, String fromFn, String fromStr, String fromC, String fromSt, int fromZ,
boolean beforeN, String d, int ship, int bSize, String msg)
{
Order newOrder = new Order(toLn, toFn, toStr, toC, toSt, toZ, fromLn, fromFn, fromStr, fromC, fromSt, fromZ,
beforeN, d, ship, bSize, msg);
orderList.add(newOrder);
return order;
}
@Override
public String printMessageCards() {
String message = "";
for (Comparable element:orderList)
{
message += ((Order) element).getMsg()+"\n"+"\n";
}
return message;
}
@Override
public String deliverySchedule() {
// TODO Auto-generated method stub
System.out.println(orderList.toString());
orderList = sortingClass.selectionSort(orderList);
System.out.println(orderList.toString());
String message = "Delivery Schedule"+"\n"+"\n";
for (Comparable element:orderList)
{
String deliveryTime = "";
if(((Order) element).getBeforeN()==true)
{
deliveryTime = "(a before noon delivery, preceeded by *)";
}
else if (((Order) element).getBeforeN()==false)
{
deliveryTime = "(an after noon delivery)";
}
message += ((Order) element).getDelivery()+"\n"+((Order) element).getToSt()+", "+((Order) element).getToC()+" - "+((Order) element).getToLn()+", "+((Order) element).getToFn()+" "+deliveryTime+"\n";
}
return message;
}
/**
* getNumOrders which is responsible for the size of the list
* @return numberOfOrders
*/
@Override
public int getNumOrders() {
// TODO Auto-generated method stub
numberOfOrders = orderList.size();
return numberOfOrders;
}
/**
* Method toString
* @return the String representation
*/
public String toString()
{
return orderList.toString();
}
}
これは私たちの先生が提供したJUnitテストです
import static org.junit.Assert.*;
import java.util.Scanner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class OrderManagerTest {
OrderManager orders1, order2;
@Before
public void setUp() throws Exception {
orders1 = new OrderManager();
orders1.addOrder("Brown", "Angela", "55321 Sycamore St.", "Gaithersburg",
"MD", 20879, "Smith", "Agnus", "344 Oak St.", "Pocatello",
"ID", 83205, true, "May 9", 1, 2, "Happy Mother's Day");
orders1.addOrder("Miller", "Karen", "3399 Campus St.", "Rockville",
"MD", 20850, "Jones", "Peggy", "5633 Meadow Way", "Bloomington",
"IN", 47404, false, "May 8", 1, 1, "Love ya");
orders1.addOrder("Hanson", "Beverly", "3356 Cypress Ln.", "North Potomac",
"MD", 20878, "Hanson", "Ken", "2985 Pointer Dr.", "Fountain Valley",
"CA", 83205, false, "May 11", 2, 3, "You're the greatest");
orders1.addOrder("White", "Carolyn", "4488 Pinewood Ave.", "Olney",
"MD", 20859, "Green", "George", "492 Apple Way", "St. Louis",
"MO", 35587, false, "May 9", 1, 2, "All the Best!");
}
@After
public void tearDown() throws Exception {
orders1 = null;
}
@Test
public void testPrintMessageCards() {
String result = orders1.printMessageCards();
Scanner scan = new Scanner(result);
assertEquals("Dear Mom", scan.nextLine());
assertEquals("Love ya", scan.nextLine());
assertEquals("Love Peggy", scan.nextLine());
scan.nextLine(); // blank line
scan.nextLine(); // blank line
assertEquals("Dear Mom", scan.nextLine());
assertEquals("Happy Mother's Day", scan.nextLine());
assertEquals("Love Agnus", scan.nextLine());
scan.nextLine(); // blank line
scan.nextLine(); // blank line
assertEquals("Dear Mom", scan.nextLine());
assertEquals("All the Best!", scan.nextLine());
assertEquals("Love George", scan.nextLine());
}
@Test
public void testAddOrder() {
assertEquals(4,orders1.getNumOrders());
orders1.addOrder("Myers", "Stephanie", "8355 Grove Ave.", "Darnestown",
"MD", 20874, "Simco", "Rebecca", "34 Charleston St.", "Orlando",
"FL", 11334, true, "May 8", 1, 1, "I miss you");
assertEquals(5,orders1.getNumOrders());
}
@Test
public void testDeliverySchedule() {
String result = orders1.deliverySchedule();
Scanner scan = new Scanner(result);
assertEquals("Delivery Schedule", scan.nextLine());
scan.nextLine(); // blank line
scan.nextLine(); // blank line
assertEquals("May 8", scan.nextLine());
assertEquals("3399", scan.next());
scan.nextLine(); // get rest of line
scan.nextLine(); // blank line
scan.nextLine(); // blank line
assertEquals("May 9", scan.nextLine());
assertEquals("*55321", scan.next());
scan.nextLine(); // get rest of line
assertEquals("4488", scan.next());
}
@Test
public void testSortClass() {
String result = orders1.printMessageCards();
Scanner scan = new Scanner(result);
assertEquals("Dear Mom", scan.nextLine());
assertEquals("Love ya", scan.nextLine());
assertEquals("Love Peggy", scan.nextLine());
// add a May 8 before noon order which should be
// sorted to be first before Peggy
orders1.addOrder("Myers", "Stephanie", "8355 Grove Ave.", "Darnestown",
"MD", 20874, "Simco", "Rebecca", "34 Charleston St.", "Orlando",
"FL", 11334, true, "May 8", 1, 1, "I miss you");
result = orders1.printMessageCards();
scan = new Scanner(result);
assertEquals("Dear Mom", scan.nextLine());
assertEquals("I miss you", scan.nextLine());
assertEquals("Love Rebecca", scan.nextLine());
scan.nextLine(); // blank line
scan.nextLine(); // blank line
assertEquals("Dear Mom", scan.nextLine());
assertEquals("Love ya", scan.nextLine());
assertEquals("Love Peggy", scan.nextLine());
}
}
testPrintMessageCards
テストは次のエラーで失敗します。expected: <[Dear Mom]> but was:<Happy Mother's Day]>
testDeliverySchedule
テストは失敗しますexpected: <M[ay 8]> but was: M[D, North Potomac - Hanson, Beverly(an after noon delivery)]>
testSortClass
テストは次のエラーで失敗します。expected: <[Dear Mom]> but was:<Happy Mother's Day]>
OrderManager
クラスで修正する必要がある正しいコードを誰かが提供できますか?