• No results found

6 Profiling and testing a Java application using TPTP

6.3 Testing a Java application with the TDD approach using TPTP

6.3.2 Setting up TPTP JUnit test suite manually

6.3.2.6 Run the test

In the Test Navigator, right-click the CustomerTest test suite and choose Run As >

…Test.

A Launch Validation Warning popup appears. Click YES to continue launch.

• Double-click the CustomerTest test log, which appears in the Test Navigator. The CustomerTest test log appears. Select the Events tab to view the test details. By expanding the Events tree, you should see the following events: start of test suite, start of Loop_Customer, test start, test verdict, test stop, loop stop, test suite verdict, and test suite stop. (see Figure 2324)

Figure 23: Test log - Events

• Click on a verdict you are interested in for more details. Alternatively you can use the navigation buttons

Figure 24: Navigation buttons to Step through the verdicts

6 Profiling and testing a Java application using TPTP

to navigate forth and back through the verdicts. In the text section next to the Events tree you will find useful messages and console output from the test execution to fix errors whenever a test fails. You can even step right into the segment of code in the test class which is causing the test to fail if you click on the corresponding error message. These are the possible types of verdicts you can select, depending on the verdicts in the test log:

error.

fail.

inconclusive.

pass.

all - Navigate through all verdicts [10].

As expected, the test failed since the method rentMovie and getTotalCharge are not

implemented yet. The verdict “error” indicates that there was an error in the code. In the next step we will eliminate the failure by implementing the missing methods.

Open the Customer.java class and enter the following code into it:

public void rentMovie(int daysRented){

}

public int getTotalCharge(){

return 2;

}

This code certainly looks very simple, but recall that our first goal is to write a code that passes the test. Whether the code does make sense or not is another issue that will be covered as we continue.

Save the file and run the test again. If you check out the verdict in the Overview tab of the Test log, you will see that it is now pass.

The test for renting one movie has succeeded. We can move on to test the renting for 2 movies. For two movies, the expected charges will be 4 Euro.

Open the CustomerTest test suite and switch to the Test Methods tab to add the

testRentingTwoMovies method. Save the test suite, choose the testRentingTwoMovies still in the Test Methods tab. Click Open and enter the following code into the method:

6 Profiling and testing a Java application using TPTP

Customer customer = new Customer();

customer.rentMovie(1);

customer.rentMovie(2);

assertEquals(4, customer.getTotalCharge());

Save the file. In the behaviour tab of the CustomerTest test suite, focus Loop_Customer and click Add->Invocation to add the testRentingTwoMovies method to the loop. Save the test suite and run the test.

Open the newly created Test log and switch to the Events tab. The verdict is fail which merely indicates that the test failed. In the Text section, check out the first error message:

Junit.framework.AssertionFailedErroe:expected:<4> but was : <2>

Open the Customer.java class and write the following lines in the testRentingTwoMovies method:

private int totalCharge = 0;

public void rentMovie(int daysRented){

Run the test and check out the verdict in the Test log. It is pass.

We consider now the case a movie is rented for more than three days. As mentioned above, every additional day will cost 1, 75 Euro from the third day on. In the next scenario, a third movie is rented for three days which does 2 Euro + 1, 75 Euro = 3, 75 Euro. The total charge will then be 7, 75 Euro. In addition we accept a tolerance up to 0,001 during comparison.

Open the Test Methods tab and add the testRentingThreeMovies method. Save the file and then add the method to the Loop_Customer. Open the method and add the following lines into it:

Customer customer = new Customer();

customer.rentMovie(1);

customer.rentMovie(2);

customer.rentMovie(3);

assertEquals(7.75, customer.getTotalCharge(), 0.001);

6 Profiling and testing a Java application using TPTP

Run the test and then take a look at the Test log to ensure yourself that it failed.

Figure 25 Test log view

If you have a look at the Events tree, you will realize that the first two tests passed and only the last one failed. In fact, the expected value is 7, 75 instead of 6 which is currently yielded by the program. Make the following changes to the Customer.java class.

private double totalCharge = 0;

public void rentMovie(int daysRented) { totalCharge += 2;

if (daysRented > 2)

totalCharge += 1.75;

}

public double getTotalCharge() { return totalCharge;

}

6 Profiling and testing a Java application using TPTP

Save the file and run the test. The verdict is still fail. This is due to the change we made to the totalCharge attribute that has moved from int to double. To get rid of the problem, update the testRentingTwoMovies as follows:

Customer customer = new Customer();

customer.rentMovie(1);

customer.rentMovie(2);

customer.rentMovie(3);

assertEquals(7.75, customer.getTotalCharge(), 0.001);

The verdict after running the test is now pass.

The fourth movie is rented for 4 days which will cost 5, 50. All the charges added together will then make 13, 25. So let’s write the test first. Once again, switch to the Test Methods tab to add testRentingFourMovies method. In the Behavior tab, choose the Loop_Customer loop and click add to add the testRentingFourMovies method into the loop. Save the test suite and then open the testRentingFourMovies to add the following lines:

Customer customer = new Customer();

customer.rentMovie(1);

customer.rentMovie(2);

customer.rentMovie(3);

customer.rentMovie(4);

assertEquals(13.25, customer.getTotalCharge(), 0.001);

Run the test to see that it expectedly failed. As next, update the Customer.java class and change the rentMovie() method as bellow:

public void rentMovie(int daysRented) { totalCharge += 2;

if (daysRented > 2)

totalCharge += (daysRented - 2)*1.75;

}

Run the test and check out the verdict. It is pass as we had expected.

Our program is so far good enough to go under production as all the tests passed.

Nevertheless it doesn’t look professional enough. There are definitely a number of points that we could improve to make the code easier to maintain.

For instance, we will replace the fixed prices with meaningful names and then declare them as instance variables as below:

6 Profiling and testing a Java application using TPTP

private double totalCharge = 0;

private final double BASE_PRICE= 2.00;

private final double PRICE_PER_DAY= 1.75;

private final int DAYS_DISCOUNTED= 2;

public void rentMovie(int daysRented) { totalCharge += BASE_PRICE;

if (daysRented > DAYS_DISCOUNTED)

totalCharge += (daysRented - DAYS_DISCOUNTED) * PRICE_PER_DAY;

}

If you have some doubt, you can still run the test for the verdict. The changes now cause another problem. As a customer can hardly have a base price or days discounted, it seems obvious that we need to create a new class that will better reflect the reality regarding the process of renting movies.

Let’s then create the class Movie.java which will be responsible for computing the charge in the future. As we are now used to, we first write the test class MovieTest.java to test

functional behaviour of the Movie.java class. We use the TPTP to create that class as we learned further above.

We use the knowledge we have got so far to add the methods testBasePrice and testPricePerDay to the CustomerTest test suite with the following codes:

testBasePrice:

Then fill in the Movie.java class with the code below:

private static final double BASE_PRICE = 2.00;

private static final double PRICE_PER_DAY = 1.75;

private static final int DAYS_DISCOUNTED = 2;

public static double getCharge(int daysRented) { double result = BASE_PRICE;

6 Profiling and testing a Java application using TPTP

result += (daysRented - DAYS_DISCOUNTED) * PRICE_PER_DAY;

}

return result;

}

In the Behaviour tab, add the methods testBasePrice and testPricePerDay to the Loop_Customer loop. Run the test suite to make sure the code still works.

The code for the Customer class now has become a lot smaller:

private double totalCharge = 0;

public void rentMovie(int daysRented) {

totalCharge += Movie.getCharge(daysRented);

}

public double getTotalCharge() { return totalCharge;

}

The class CustomerTest class has to be updated in the following way as well:

private Customer customer;

protected void setUp() throws Exception { customer = new Customer();

}

public void testRentingOneMovie() throws Exception { customer.rentMovie(1);

assertEquals(2.00, customer.getTotalCharge());

}

public void testRentingThreeMovies() throws Exception { customer.rentMovie(2);

customer.rentMovie(3);

customer.rentMovie(4);

assertEquals(11.25, customer.getTotalCharge(), 0.001);

}

6 Profiling and testing a Java application using TPTP

The methods testRentingTwoMovies and testRentingFourMovies have been removed from the Loop_Customer as their duplicate the methods testRentingOneMovies and

testRentingThreeMovies.

Save all the changes in order for them to take effect and run the test afterward. Open the Test log for the verdict. The verdict is “pass” for all the executed test cases.

Figure 26 Test log view

Our code can now be considered “perfect” as it passed all the tests it underwent and thus fulfil the requirements as specified above.

In this tutorial we learned how to stick to the TDD technique using the TPTP platform. You can now play around with the tools by developing and testing other examples with more confidence. The example we took here just presents a couple a features the TPTP provides.

You might play around with the tools to discover more.

Related documents