Presentation is loading. Please wait.

Presentation is loading. Please wait.

Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.

Similar presentations


Presentation on theme: "Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to."— Presentation transcript:

1 Well-behaved objects 4.0 Testing

2 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered Testing Debugging Test automation Writing for maintainability

3 3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Code snippet of the day public void test() { int sum = 1; for (int i = 0; i <= 4; i++); { sum = sum + 1; } System.out.println("The result is: " + sum); System.out.println("Double result: " + sum+sum); } What is the output?

4 4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Results The result is: 6 The result is: 11 The result is: 5 The result is: 2 Double result: 12 Double result: 4 Double result: 22 Double result: 66 The result is: 6 Double result: 66 Which one is printed?

5 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling We have to deal with errors Early errors are usually syntax errors. –The compiler will spot these. Later errors are usually logic errors. –The compiler cannot help with these. –Also known as bugs. Some logical errors have no immediately obvious manifestation. –Commercial software is rarely error free.

6 6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection (Developer vs Maintainer) We can lessen the likelihood of errors. –Use software engineering techniques, like encapsulation. We can improve the chances of detection. –Use software engineering practices, like modularization and documentation. We can develop detection skills.

7 7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Testing and debugging These are crucial skills. Testing searches for the presence of errors. Debugging searches for the source of errors. –The manifestation of an error may well occur some ‘distance’ from its source.

8 8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Testing and debugging techniques Unit testing (within BlueJ) Test automation Manual walkthroughs Print statements Debuggers

9 9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Unit testing Each unit of an application may be tested. –Method, class, module (package in Java). Can (should) be done during development. –Finding and fixing early lowers development costs (e.g. programmer time). –A test suite is built up.

10 10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Testing fundamentals Understand what the unit should do – its contract. –You will be looking for violations. –Use positive tests and negative tests. Test boundaries. –Zero, One, Full. Search an empty collection. Add to a full collection.

11 Well-behaved objects Test automation

12 12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered Unit testing JUnit Regression testing Test cases Test classes Assertions Fixtures

13 13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Unit testing within BlueJ Objects of individual classes can be created. Individual methods can be invoked. Inspectors provide an up-to-date view of an object’s state. Explore through the diary-prototype project.

14 14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Test automation Good testing is a creative process, but...... thorough testing is time consuming and repetitive. Regression testing involves re-running tests. Use of a test rig or test harness can relieve some of the burden. –Classes are written to perform the testing. –Creativity focused in creating these.

15 15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Test automation Explore through the diary-testing project. –Human analysis of the results still required. Explore fuller automation through the diary-test-junit projects. –Intervention only required if a failure is reported.

16 16 Example Suppose a program must accept the price of an orange, the number of oranges requested and generate the total cost. –The price of an orange, in pence, will be less than 100 –The number of oranges will be in the range 1 to 20 inclusive. –A suitable output should result if either input entry is invalid.

17 17 Using inspectors Once an object has been created, the value of it’s fields can be monitored using the inspect facility (accessed by right click on object). It is most important when data structures are being used to check their behaviour when empty as well as when full.

18 18 Class: Bill public class Bill { // instance variables private int price; private int quantity; /** * Constructor of class Bill */ public Bill(int price, int quantity ) { // initialise instance variables this.price = price; this.quantity = quantity; } /** *Method to determine bill amount * * @return Bill total */ public int billTotal() { if((price>0)&&(price<100)) { if((quantity>0)&&(quantity<21)) { return price * quantity; } return -999; }

19 19 Create unit test Use Create Test Method to create a series of test cases along with the expected results and stored as test methods within the unit test class.

20 20 Test case This will involve the creation of one or more objects of the class being tested. These appear as instantiations in the respective test methods. The assertion is introduced as part of the recording process. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public void testCase1() { Bill bill1 = new Bill(1, 1); assertEquals(1, bill1.billTotal()); }

21 21 Running tests Individual tests may be run from the unit test menu: Alternatively all tests may be performed by selecting TestAll. Results are checked against expected outcomes and reported as appropriate:

22 22 Fixtures To instantiate objects required by all tests Accomplished using Object Bench to Test Fixture –SetUp() method executed before any of the test methods. –declares fields for each instantiation and invokes corresponding constructors within SetUp() method. –objects appear on object bench Test Fixture to Object Bench. –loads objects onto bench from SetUp().

23 23 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Demo

24 24 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling JUnit JUnit is a Java test framework Test cases are methods that contain tests Test classes contain test methods Assertions are used to assert expected method results Fixtures are used to support multiple tests


Download ppt "Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to."

Similar presentations


Ads by Google