Results 1 to 6 of 6
  1. #1
    prabhdsun is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default JUnit test - comparison problems.

    Hi, for this assignment our teacher provided us with a J Unit test that we had to match with our code. This is the class i wrote - OrderManager
    Java Code:
    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();
    	}
    }
    This is the J unit test that our teacher provide - OrderManagerTest
    Java Code:
    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());
    	}
    
    }
    Here are the tests that i am failing.

    J unit test fails testPrintMessageCards - which says ...:expected: <[Dear Mom]> but was:<Happy Mother's Day]>
    J unit test fails testDeliverySchedule - which says.....:expected: <M[ay 8]> but was: M[D, North Potomac - Hanson, Beverly(an after noon delivery)]>
    J unit test fails testSortClass - which says........ :expected: <[Dear Mom]> but was:<Happy Mother's Day]>

    Can anyone provide the correct code that i need to fix in my OrderManager class?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,239
    Rep Power
    19

    Default Re: JUnit test - comparison problems.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,239
    Rep Power
    19

    Default Re: JUnit test - comparison problems.

    Quote Originally Posted by prabhdsun View Post
    Can anyone provide the correct code that i need to fix in my OrderManager class?
    No, we are not a code factory. Also, we don't condone cheating on homework.

    Ask a specific question and you nay get help. As it stands, your post is just a homework dump.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    prabhdsun is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: JUnit test - comparison problems.

    im sorry, is that not allowed in this forum? if it isnt ill take it off the stackoverflow website.

  5. #5
    prabhdsun is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: JUnit test - comparison problems.

    What i mean by the correct code is, give me pointers as to where im going wrong so i can fix it. and how am i cheating here?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default Re: JUnit test - comparison problems.

    Not knowing your requirements it's not exactly easy to say.
    Looks to me like you've missed out a step or two.
    All the messages seem to require a "Dear Mom" first line, and also to be ordered by date.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JUnit test nullPointerException
    By danboy4 in forum New To Java
    Replies: 1
    Last Post: 10-10-2012, 07:06 AM
  2. problem with JUnit test
    By exltus in forum Advanced Java
    Replies: 12
    Last Post: 12-19-2011, 07:49 PM
  3. Replies: 1
    Last Post: 08-03-2009, 05:31 PM
  4. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 05:15 PM
  5. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 01:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •