assenger p, Seat s, double price) Before you create the Ticket object: i. Use the wizard to generate the TicketTest class ii. Add a test method to the class. iii. In the test method, create a Passenger object. The default constructor of the Passenger class
Create a second test class called TicketTest to test that a Ticket object is
constructed correctly when provided reasonable input.
a. Write a test method that calls the following constructor with valid arguments:
Ticket(Passenger p, Seat s, double price)
Before you create the Ticket object:
i. Use the wizard to generate the TicketTest class
ii. Add a test method to the class.
iii. In the test method, create a Passenger object. The default constructor
of the Passenger class generates a valid name “T. B. A.”, so either of
the following statements instantiate a Passenger:
Passenger passenger = new Passenger();
Or:
Passenger passenger = new Passenger(
new PassengerName(“Mary”, “I”, “Worth”));
iv. Create a Seat object.
The Seat class has no explicit constructor so you must use the implicit
default constructor and then call seat.setRow(int i) and
seat.setLetter(char c) to specify a specific seat.
Unit Testing with JUnit
v. Create the ticket object, providing the passenger object, seat object and the price as a number as the three arguments of the constructor.
Ticket ticket = new Ticket(passenger, seat, price);
b. For a condition that verifies that a ticket is issued, use the fact that tickets are numbered, starting at 1,000,000. So lines in the test method could look like:
assertTrue(ticket.getTicketNo() > 1000000 );
c. Write a second test in the same class that generates a ticket when called with arguments that are not valid. In real life, you should create several tests to isolate different combinations of valid and invalid input values, but for purposes of this exercise combine several tests, as in:
@Test
public void testTicketBad() {
assertThrows(IllegalArgumentException.class, ()->{
Ticket ticket = new Ticket(null, null, -100.0);} );
}
d. Run the test class as a JUnit test and take note of the results.
Hint: The test fails because the second test method does not throw an exception where clearly one should be thrown. This defect should be reported. It would helpful for the test report suggest more tests that give better coverage invalid input.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 7 images