I am having a few minor issues with my class methods which I am to increment sequentially. Basically my class compiles, there is a test class and when I run the tester to get the test results I do not get the desired output I am looking for and it is followed by an error message which I will provide below.
My question is when I create an object it starts off with the the prefix + counter but does not increment it right away, I want it to be so that it increments the first object created to 1001 right away and so on and so forth. These error messages are confusing me on how the expected value is something totally different from my desired output.
I have the prefix set and a counter which starts at 1000.
public static final String TICKET_PREFIX = "VAN";
public static int counter = 1000;
public ParkingTicket(){
ticketNumber = generateTicketNumber();
}
}
private String generateTicketNumber(){
ticketNumber = TICKET_PREFIX + counter++;
return ticketNumber;
}
Here is the code from the test class code block where the error is caught
@Test
public void testConstructorTicketNumberSEquential() {
ParkingTicket.resetCounter();
ParkingTicket ticket = new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 );
ParkingTicket ticket2 = new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 );
ParkingTicket ticket3 = new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 );
assertEquals("VAN1001",ticket.getTicketNumber());
assertEquals("VAN1003", ticket3.getTicketNumber());
and this is the error message from the tester
testConstructorTicketNumberSEquential
---
org.junit.ComparisonFailure: expected:<VAN100[1]> but was:<VAN100[7]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at ParkingTicketTest.testConstructorTicketNumberSEquential(ParkingTicketTest.java:238)
Also I have a method to reset the counter. I tried just returning the counter value but I am still getting an error
public static int resetCounter(){
int reset = counter;
return reset;
}
test for the reset counter
@Test
public void testResetCounter() {
ParkingTicket.resetCounter();
assertEquals(1000,ParkingTicket.counter);
}
test run error message
testResetCounter
---
java.lang.AssertionError: expected:<1000> but was:<1005>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:555)
at org.junit.Assert.assertEquals(Assert.java:542)
at ParkingTicketTest.testResetCounter(ParkingTicketTest.java:264)