我的老师为我们提供了该课程的 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
课堂上修复的正确代码?