Results 1 to 7 of 7
  1. #1
    pokadean is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default Question about my program and JUnit tests

    Hi! guys! I have two questions regarding my program; if you please could help me understand what I do wrong it would be awesome!

    So, my first problem is a failure that's been given to me when the Junit test tries to do this:
    Java Code:
    	    public void testflightsEmpty() {
    	        WorldAirports am = new WorldAirports();
    	        Company lu = new Company("Lufthansa", am);
    	        Airport a1 = new Airport("London");
    	        Airport a2 = new Airport("Paris");
    	        try{
    	            am.addAirport(a1);
    	            am.addAirport(a2);
    	            lu.addAirplane("734", 150);
    	            lu.addFlight("LH359", "734", "London", "Paris", "Tuesday");
    	            lu.cancelFlight("LH359");
    	        }catch(InvalidCode ic){
    	            fail("Il codice dovrebbe essere valido");
    	        }
    	        assertTrue(am.getAirport("London").getDepartures().isEmpty());
    	        assertTrue(am.getAirport("London").getArrivals().isEmpty());
    	    }
    And the test stops at the first of the two " isEmpty() "
    Now, what I tried to do is the following:
    I will not post the entire code by the time it is very long but just so you know all the other tests regarding the creation of the airport and stuff are correct so everything is fine
    Java Code:
    	private List<Flight> flightsdept = new LinkedList<Flight>();
    
    	public void getDepartingFlights(Flight f){
    		flightsdept.add(f);
    	}
    
    
    	public List<Flight> getDepartures(){
    		Collections.sort(flightsdept);
    		return flightsdept;
    	}
    In my Airport class I have these two methods, implementing what it should be a list of dept and arriving flights and other tests give this to me as right!
    So the problem is the fact that the test is not actually seeing my list as empty right?

    you guys have any Idea?

    -------

    Now, the other problem is kinda bit weird and is the following:
    I'm trying to see for the flights that have free seats bigger than the 30% of the total seats.

    Java Code:
    	public void testCriticalFlights() {
    		try {
    			assertTrue(lu.reserve(20, "LH358"));
    			assertTrue(lu.reserve(40, "LH359"));
    			assertTrue(lu.reserve(10, "LH567"));
    			assertTrue(lu.reserve(100, "LH274"));
    			lu.flightDeparted("LH358", 10);
    			lu.flightDeparted("LH567", 16);
    			lu.flightDeparted("LH274", 20);
    		}catch(InvalidCode ic){
    		}
    
    		assertEquals(lu.criticalFlights().size(), 2);
    		assertEquals(lu.criticalFlights().get(0),"LH358");
    		assertEquals(lu.criticalFlights().get(1),"LH567");
    	}
    This is the JUnit and it stops when it's not recognizing the exact size of my list that is says should be 2.

    In order to implement this part I did the following:

    Java Code:
    	List<String> critic = new LinkedList<String>();
    
    
    	public List<String> criticalFlights() {
    		for (String f : flights.keySet()){
    			Flight d=flights.get(f);
    	if(d.returnPassengers() < ((planes.get(d.getAirplanecode()).returnAvailableSeats()/100)*30) ){
    		critic.add(f);
    	}
    		
    	}
    		Collections.sort(critic);
    		return critic; 
    }
    where planes is the Map of airplanes I have, returnPassengers gives me the free seats and availableSeats returns me the total seats available in the Airplane.

    Thank you so much for the patience of reading through all this!

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Question about my program and JUnit tests

    For the first one, since all the other tests to this point work (which I assume means tests that show all the methods up to cancel() work) then I would look at the cancel code.
    Which you haven't shown us.

    Can't tell for the second part as there's too much missing that could be going wrong.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Question about my program and JUnit tests

    I think it is time to learn how to properly use a debugger to step through the unit test and see what is going on.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    pokadean is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default Re: Question about my program and JUnit tests

    Ok! I figured out one of the error!
    I was so dumb i didn't add methods to actually remove the flights from the lists of flights of the airport once i eliminated the flight itself!
    sorry if I write here again but you guys help me sooo much to realize my errors!
    one failure left! hahah

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Question about my program and JUnit tests

    Note: when people refer to themselves as dumb, they're actually really only a bit impatient ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    pokadean is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default Re: Question about my program and JUnit tests

    haha that's exactly my case, I lack patience and the minute I see there's something wrong I start saying "i don't know what is wrong, everything is right!"

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Question about my program and JUnit tests

    Be honest to yourself. It is more like: "I don't want anything to be wrong, everything has to be right so I am not bothered!". Am I right? Be brutal :)

    I'm always right so in this case I am going to assume it is true again: that will go away if you spend some time learning how you can research misery. And my earlier suggestion of learning how to use a debugger is paramount to you becoming less afraid of/bothered by things being not as you expect them to be.

    *sniff sniff*

    What stinks all of a sudden?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. New to Java 2: Simple game and a Junit question.
    By tzzt123 in forum New To Java
    Replies: 1
    Last Post: 05-13-2013, 07:44 AM
  2. Have JUnit tests, need help developing methods
    By alpdog14 in forum New To Java
    Replies: 20
    Last Post: 03-24-2011, 08:27 AM
  3. Junit tests with ant
    By questioner in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 10:25 AM
  4. Remote Junit Tests
    By lord.ec in forum Eclipse
    Replies: 0
    Last Post: 12-09-2008, 08:15 AM
  5. Using ant to run JUnit tests
    By racerxadam in forum Advanced Java
    Replies: 0
    Last Post: 10-21-2008, 04:48 PM

Posting Permissions

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