I have to make an IT on the entry in a parking lot (a ticket must be recorded in my DB and the places updated) and an IT on the exit of this same parking lot with the same vehicle (the place must be updated in the DB with the exit time the price).
At this stage, I succeeded to do my first IT, but the on second one I have 2 problems:
- The price remains at 0;
- The vehicle goes out and comes in again directly, so the DB is not updated correctly.
Here is the code :
@Test
public void testParkingACar(){
ParkingService parkingService = new ParkingService(inputReaderUtil, parkingSpotDAO, ticketDAO);
parkingService.processIncomingVehicle();
//TODO: check that a ticket is actually saved in DB and Parking table is updated with availability
Date inTime = new Date(System.currentTimeMillis() - ( 45 * 60 * 1000));
Ticket ticket = ticketDAO.getTicket("ABCDEF");
ticket.setInTime(inTime);
Assert.assertNotNull(ticket.getInTime());
}
@Test
public void testParkingLotExit(){
testParkingACar();
ParkingService parkingService = new ParkingService(inputReaderUtil, parkingSpotDAO, ticketDAO);
parkingService.processExitingVehicle();
// TODO: check that the fare generated and out time are populated correctly in
// the database
Ticket ticket = ticketDAO.getTicket("ABCDEF");
Assert.assertNotNull(ticket.getPrice());
Assert.assertNotNull(ticket.getOutTime());
assertEquals(0.75*Fare.CAR_RATE_PER_HOUR, ticket.getPrice());
}
I hope I've been clear, if you have any questions about certain methods/variables don't hesitate.
CodePudding user response:
You are seeing multiple issues here. (I'll assume you use JUnit as the syntax looks somewhat familiar)
- Firstly the execution order of Junit tests is not predicatable but deterministic. This means you don't know if a car leaves and enters or enters and leaves, you have no control over the order.
- Secondly, you have 2 instances of
ParkingService
one intestParkingACar
and one intestParkingLotExit
seenew ParkingService(...);
. - Thirdly, as user tgdavies meantioned in the comment you make a call from one unit test to another, this should be avoided.
I would recomment you use one unit test instead of 2 to test the basic functionality, this scenario is a little challanging since you need to wait for a specified amount of time but this time is not exact and you shouldn't count on that for repeatability.
CodePudding user response:
I tried to inject a custom clock but my in-time recorded was equals to my out-time recorded , don't understand why.
Finally, I used this code: I have to wait 5 seconds for my test but it works correctly. And for the price to be up to date in the database, I just have to change the price calculation method and put it in seconds. This is the best solution I found, hopefully it's enough to pass my exam ;)
@Test
public void testParkingLotExit(){
testParkingACar();
ParkingService parkingService = new ParkingService(inputReaderUtil, parkingSpotDAO, ticketDAO);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
parkingService.processExitingVehicle();
//TODO: check that the fare generated and out time are populated correctly in the database
Ticket ticket = ticketDAO.getTicket("ABCDEF");
Assert.assertNotNull(ticket.getPrice());
Assert.assertNotNull(ticket.getOutTime());
}
Thanks again for your precious help!