Software Testing for Object Oriented Design

What is the rationale behind object oriented design?

Before talking about testing perspective, let’s briefly talk about object oriented design. we can not grasp the object oriented design by simply learning about inheritance, polymorphism, association, composition, aggregation, abstraction and encapsulation etc. These concepts are merely alphabets for OOP. The OOP design is a total paradigm shift. You need to think in terms of objects, the objects which are independent and very loosely coupled.

Do you know what is the motive behind object oriented paradigm? The most important motive is re-usability.The cohesiveness leads to decoupling and the decoupling leads to re-usability. To better understand the concept, let’s take an example. Lets say we have three class.

  1. AC switch with options on/off
  2. Fan switch with options off/low/med/high
  3. AC

In above scenario, the AC will be ON if the AC switch is ON and the fan switch is not in off mode. if either the AC switch or the fan switch is off, the AC will be OFF. Suppose, you have to write three class for above three entities. You know, when we write a code for some application, one of the classes controls the other classes. so we have four options.

  1. The AC switch and the fan switch classes report to AC class and the AC class acts accordingly.
  2. The AC switch becomes “smart”. When its status is changed, it gets the status of fan switch and controls the AC accordingly. [Here is a problem: when fan switch is changed. the AC switch can not know about it.]
  3. The fan switch becomes “smart”. When its status is changed, it gets the status of AC switch and controls the AC accordingly. [Here is a problem: when AC switch is changed. the fan switch can not know about it.]
  4. The AC class keeps on checking the status of both other classes after every second and when some change is seen, it acts accordingly.

Now, you know, there is a technical issue or bug in 2 and 3 options but what about 1 and 4 options, which is the best?? Here the paradigm of decoupling and re-usability comes to rescue. In option 4, the classes are more tightly coupled as compared to option 1. So the winning option is the option 1. This is how we decide among multiple options of object oriented design. You can study all the accepted and popular design patterns. The rationale behind every design pattern is the decoupling and re-usability.

Testing Techniques

While testing the object oriented code, the first question may be, which class should be tested first. The rule of testing order is very simple and based on common sense. The independent class should be tested first and the dependent class should be tested later. For instance, let’s say, class A has association with class B. Say, class A uses class B. In this case, class B is independent and class A is dependent on Class B. So we would test class B first.

Based on this tool, here are the conclusions.

  1. In case of whole – part relationship, the part would be tested first. The examples are aggregation and composition.
  2. In case of inheritance, the parent class would be tested first as it is independent of the child class.

To further explain the concept, let’s study an example.

OOP Testing
Figure-1 OOP Testing

In this figure-1, there may be more than one orders of testing using the above logic.

  1. X then C then Y then D then Z
  2. C then D then X then Y then Z

The bottom line is, we will always test the independent first and the dependent later on.

How to write test cases for OOP testing

While testing the OOP, our focus should be object not the independent statements of code. we will test the object by using its methods and we call methods by passing arguments. we evaluate the test case by previous and post states of the object.  let’s look at below class

Now, if we change the width, the state of the object would be changed. it will not only effect the data member width but also the data member area. Here is the test case table for object oriented testing. say the previous state of the object was width=10 and area 100. After calling the method “setwidth” with argument 20, the post state should be width=20 and area=400. A sample test case is written below.

Id Input Previous state Expected post state Actual post state Expected output Actual output Exception if any Status
1 Setwidth(20) 10,100 20,400