Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Chapter 7 Testing Class Hierarchies. SWE 415 Chapter 7 2 Reading Assignment  John McGregor and David A. Sykes, A Practical Guide to Testing Object-Oriented.
5/17/2015 OO Design: Liskov Substitution Principle 1.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Software Testing and Quality Assurance Lecture 28 – Testing Class Hierarchies.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 10 Classes Continued
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
More Design. Next Tuesday First draft of architectural design –use cases –class diagrams for major classes with responsibilities –sequence diagrams for.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Software Design Principles
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Test Code Patterns How to design your test code. 2 Testing and Inheritance Should you retest inherited methods? Can you reuse superclass tests for inherited.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
Phil Tayco Slide version 1.0 Created Nov 6, 2017
EECE 310: Software Engineering
Interfaces and Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
Subtype Polymorphism, Subtyping vs
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Presentation transcript:

Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?

2 Testing and Inheritance Should you retest inherited methods? Can you reuse superclass tests for inherited and overridden methods? To what extent should you exercise interaction among methods of all superclasses and of the subclass under test?

3 Inheritance In the early years people thought that inheritance will reduce the need for testing Claim 1: “If we have a well-tested superclass, we can reuse its code (in subclasses, through inheritance) with confidence and without retesting inherited code” Claim 2: “A good-quality test suite used for a superclass will also be good for a subclass” Both claims are wrong.

4 Inheritance-related bugs Incorrect Initialization class Parent{ public void reset(){ initialize(); … } void initialize(){ //Initialize //Parent’s //attributes } class Child{ void initialize(){ //Initialize //Child’s //attributes } reset will work incorrectly in Child unless initialize calls its superclass version

5 Inheritance-related bugs Inadvertent Bindings class Child{ int a=3; } class Parent{ int a=5; void print(){ System.out.print(a); } What will be printed? Is it the intended value? class Driver{ … Child c = new Child(); c.print(); }

6 Inheritance-related bugs Missing Override A subclass omits to provide a specialized version of a superclass method Subclass objects will have to use the superclass version, which might not be appropriate E.g. method equals in Object tests for reference equality. In a given class, it might be right to override this behaviour

7 Inheritance-related bugs Direct access to superclass fields from the subclass code Changes to the superclass implementation can create subclass bugs Subclass bugs or side effects can cause failure in superclass methods If a superclass is changed, all subclasses need to be tested If a subclass is changed, superclass features used in the subclass must be retested

8 Inheritance-related bugs Square Peg in a Round Hole Design Problem A subclass is incorrectly located in a hierarchy Liskov substitution principle: Functions that use references to base classes must be able to use objects of derived classes without knowing it.

9 An example Consider class Rectangle below class Rectangle{ public void setWidth(double w) {itsWidth=w;} public void setHeight(double h) {itsHeight=w;} public double getHeight() {return itsHeight;} public double getWidth() {return itsWidth;} private double itsWidth; private double itsHeight; };

10 An example Assume that the system containing Rectangle needs to deal with squares as well Since a square is a rectangle, it seems to make sense to have a new class Square that extends Rectangle That very “reasonable” design can cause some significant problems

11 Problems with this design Do not need both itsHeight and itsWidth setWidth and setHeight can bring a Square object to a corrupt state (when height is not equal to width) class Square{ setWidth(double w){ super.setWidth(w); super.setHeight(w); } // Similar for setHeight } One solution

12 Not really a solution Consider this client code The problem is definitely not with the client code Rectangle r; … r.setWidth(5); r.setHeight(4); assert(r.getWidth() * r.getHeight()) == 20);

13 What went wrong? The Liskov substitution principle was violated If you are expecting a rectangle, you can not accept a square The overridden versions of setWidth and setHeight broke the postconditions of their superclass versions Isn’t a square a rectangle? Yes, but not when it pertains to its behaviour

14 Inheritance-related bugs Worm Holes A subclass violates an invariant from the superclass Spaghetti Inheritance Complicated multiple inheritance may lead to the wrong variable or method being inherited

15 Inheritance-related bugs Fat Interface A subclass inherits methods that are inappropriate or irrelevant class Money extends BigDecimal{ // movePointLeft should probably be // overridden to do nothing } class BigDecimal{ BigDecimal movePointLeft(int n) {..} }

16 Testing of Inheritance Principle: inherited methods should be retested in the context of a subclass Example 1: if we change some method m in a superclass, we need to retest m inside all subclasses that inherit it

17 Example 2 If we add a new method m2 that has a bug and breaks the invariant, method m is incorrect in the context of B even though it is correct in A Therefore, m should be tested in B class A { int x; // invariant: x > 100 void m() { // correctness depends on // the invariant } } class B extends A { void m2() { x = 1; } }

18 Example 3 If inside B we override a method from A, this indirectly affects other methods inherited from A e.g., method m calls B.m2, not A.m2: so, we cannot be sure that m is correct anymore and we need to retest it inside B class A { void m() { …; m2(); … } void m2() { … } } class B extends A { void m2() { … } }

19 Testing of Inheritance (cont) Test cases developed for a method m defined in class X are not necessarily sufficient for retesting m in subclasses of X e.g., if m calls m2 in A and then some subclass overrides m2 we have a completely new interaction that may not be covered well by the old test cases for m Still it is essential to run all superclass tests on a subclass Goal: check behavioral conformance of the subclass w.r.t. the superclass (LSP)

20 Effect of Inheritance on Testing? Does not reduce the volume of test cases Rather, number of interactions to be verified goes up at each level of the hierarchy

21 Polymorphic Server Test Consider all test cases that exercise polymorphic methods According to LSP, these should apply at every level of the inheritance hierarchy Expand each test case into a set of test cases, one for each polymorphic variation

22 An example class TestAccount { Account public void setUp(){ a = new Account(); public final void testDeposit(){ a.deposit(100); assertTrue(a.getBalance()==100); }

23 An example class TestSavingsAccount extends TestAccount{ SavingsAccount public void setUp(){ a = new SavingsAccount(); sa = new public void testInterest(){ sa.deposit(100); sa.applyInterest(); assertTrue(sa.getBalance()==101); }}

24 Testing abstract classes Abstract classes cannot be instantiated However, they define an interface and behaviour (contracts) that implementing classes will have to adhere to We would like to test abstract classes for functional compliance Functional Compliance is a module's compliance with some documented or published functional specification

25 Functional vs. syntactic compliance The compiler can easily test that a class is syntactically compliant to an interface All methods in the interface have to be implemented with the correct signature Tougher to test functional compliance A class implementing the interface java.util.List may be implementing get(int index) or isEmpty() incorrectly Think LSP…

26 Abstract Test Pattern This pattern provides the following A way to build a test suite that can be reused across descendants A test suite that can be reused for future as-yet-unidentified descendants Especially useful for writers of APIs.

27 An example Consider a statistics application that uses the Strategy design pattern public interface StatPak { public void reset(); public void addValue(double x); public double getN(); public double getMean(); public double getStdDev(); }

28 Abstract Test Rule 1 Write an abstract test class for every interface and abstract class An abstract test should have test cases that cannot be overridden It should also have an abstract Factory Method for creating instances of the class to be tested.

29 Example abstract test class public abstract TestStatPak { private StatPak public final setUp() throws Exception { statPak = createStatPak(); assertNotNull(statPak); } // Factory Method. Every test class of a // concrete subclass K must override this // to return an instance of K public abstract StatPak createStatPak(); //Continued in next slide…

30 Example abstract test public final void testMean() { statPak.addValue(2.0); statPak.addValue(3.0); statPak.addValue(4.0); statPak.addValue(2.0); statPak.addValue(4.0); assertEquals("Mean value of test data should be 3.0", 3.0,statPak.getMean()); public final void testStdDev() {... }}

31 Abstract Test Rule 2 Write a concrete test class for every implementation of the interface (or abstract class) The concrete test class should extend the abstract test class and implement the factory method

32 Example concrete test class public class TestSuperSlowStatPak extends TestStatPak { public StatPak createStatPak() { return new SuperSlowStatPak(); } Only a few lines of code and all the test cases for the interface have been reused

33 Guideline Tests defining the functionality of the interface belong in the abstract test class Tests specific to an implementation belong in a concrete test class We can add more test cases to TestSuperSlowStatPak that are specific to its implementation