I have the following two classes:
public class Station{
private String id;
private String description;
private boolean isTerminus;
private int numberPassengersInTransit;
public Station(String id, String desc) {
this.id = id;
description= desc;
}
public String getDescription() {
return descrizione;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isTerminus() {
return isTerminus;
}
public void setTerminus(boolean isTerminus) {
this.isTerminus = isTerminus;
}
public String getId() {
return id;
}
public int getNumberPassengersInTransit() {
return numberPassengersInTransit;
}
public void addPassenger() {
numberPassengersInTransit++;
}
public void addGroupPassengers(int num) {
numberPassengersInTransit += num;
}
}
and
public class Train {
private String id;
private String description;
private Station parkingPlace;
private int numberPassengersOnBoard;
public final int NUMBER_MAX_PASSENGERS = 200;
private StringBuilder log = new StringBuilder();
public Train(String id, String desc, Station parkingPlace) {
this.id = id;
descrizione = desc;
this.parkingPlace = parkingPlace;
}
public Station getParkingPlace() {
return parkingPlace;
}
public void setParkingPlace(Station parkingPlace) {
this.parkingPlace = parkingPlace;
}
public int getNumberPassengersOnBoard() {
return numberPassengersOnBoard;
}
public String getId() {
return id;
}
public String getDescription() {
return description;
}
public void runs(Station arrival) {
log.append("The train "+id+" travel to the station "+parkingPlace.getDescription()+" at the station "+arrival.getDescription()+".");
parkingPlace=arrival;
}
public void addPassenger() {
if(numberPassengersOnBoard<NUMBER_MAX_PASSENGERS)
numberPassengersOnBoard++;
else
System.out.println("Error");
}
public void addGroupPassengers(int num) {
if(numberPassengersOnBoard+num<NUMBER_MAX_PASSENGERS)
numberPassengersOnBoard++;
else
System.out.println("Error");
}
public String getLog() {
return log.toString();
}
}
The test classes:
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class StationTest {
private Station sta;
@Before
public void setUp() throws Exception {
sta = new Station("STRM0001","Giardinetti");
}
@Test
public void testConstructorStation() {
assertEquals("The station id was not initialized correctly",
"STRM0001", sta.getId());
assertEquals("The description from the station was not initialized correctly",
"Giardinetti", sta.getDescription());
assertEquals("At creation the number of passengers in transit is 0.",
0, sta.getNumberPassengersInTransit());
}
@Test
public void testAddPassenger() {
sta.addPassenger();
assertEquals("After adding a passenger for the first time, the number of passengers in transit must be 1.",
1, sta.getNumberPassengersInTransit());
}
@Test
public void testAddGroupPassengers() {
sta.addGroupPassengers(5);
assertEquals("After adding a group of passengers for the first time, the number of passengers in transit must be consistent.",
5, sta.getNumberPassengersInTransit());
}
@Test
public void testAddPassengerAndThenGroupPassengers() {
sta.addPassenger();
sta.addGroupPassengers(3);
assertEquals("After adding a passenger and then a group of passengers" +
" the number of passengers in transit must be consistent.",
4, sta.getNumberPassengersInTransit());
}
@Test
public void testAggiungiGruppoPasseggeriEPoiPasseggero() {
sta.addGroupPassengers(2);
sta.addPassenger();
assertEquals("After adding a passenger and then a group of passengers" +
" the number of passengers in transit must be consistent.",
3, sta.getNumberPassengersInTransit());
}
}
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class TrenoTest {
private Train trainTest;
private Station stationTest;
@Before
public void setUp() throws Exception {
trainTest = new Train("testRM001", "Yellow arrow", stationTest);
}
@Test
public void testConstructorTrain() {
assertEquals("L'id from the train was not initialized correctly",
"testRM001", trainTest.getId());
assertEquals("The description from the train was not initialized correctly",
"Yellow arrow", trainTest.getDescription());
assertEquals("The train station was not initialized correctly. ",
stationTest, trainTest.getParkingPlace());
}
@Test
public void testRuns() {
Station st1 = new Station("STRM0001","Giardinetti");
trainTest.setParkingPlace(st1);
Station st2 = new Station("STRM0002","Termini");
assertEquals("The departure station does not coincide with the train station.",
st1, trainTest.getParkingPlace());
trenoTest.effettuaCorsa(st2);
assertEquals("The arrival station does not coincide with the train station.",
st2, trainTest.getParkingPlace());
String log = "The testRM001 train travels from Giardinetti station to Termini station.";
assertEquals("The log does not report correct information.", log, trainTest.getLog());
}
@Test
public void testAddPassenger() {
trainTest.addPassenger();
assertEquals("After adding a passenger for the first time, the number of passengers in transit must be 1.",
1, trainTest.getNumberPassengersOnBoard());
}
@Test
public void testAddGroupPassengers() {
trainTest.addGroupPassengers(5);
assertEquals("After adding a group of passengers for the first time, the number of passengers in transit must be consistent.",
5, trainTest.getNumberPassengersOnBoard());
}
@Test
public void testAddPassengerAndThenGroupPassengers() {
trainTest.addPassenger();
trainTest.addGroupPassengers(3);
assertEquals("After adding a passenger and then a group of passengers" +
" the number of passengers in transit must be consistent.",
4, trainTest.getNumberPassengersOnBoard());
}
@Test
public void testAddGroupPassengersAndThenPassenger() {
trainTest.addGroupPassengers(2);
trainTest.addPassenger();
assertEquals("After adding a passenger and then a group of passengers " +
" the number of passengers in transit must be consistent.",
3, trainTest.getNumberPassengersOnBoard());
}
}
Running the two classes I get the following errors:
- After adding a passenger and then a group of passengers, the number of passengers in transit must be consistent. expected: <3> but was: <2>.
- After adding a group of passengers for the first time, the number of passengers in transit must be consistent. expected: <5> but was: <1>.
- After adding a passenger and then a group of passengers, the number of passengers in transit must be consistent. expected: <4> but was: <2>.
How can I fix the three errors above?