dimanche 3 avril 2016

Testing: How can we test a class implements many interface

My question is about testing a class that implements many interface. For example, I have this class:

public class ServiceControllerImpl extends ServiceController implements IDataChanged, IEventChanged {

}

Now there are two way for testing. Firstly is testing directly on concreted class. That means the object type is concreted class rather than interface.

public class ServiceControllerImplTest {
    ServiceControllerImpl instance;
    @Before
     public void setUp() {
         instance = new ServiceControllerImpl();
         // you can bring this instance anywhere
     }
}

Second way is testing on interface only, we must typecast this object to all interface it implements.

public class ServiceControllerImplTest {
    ServiceController instance;       // use interface here 
    IDataChanged dataChangeListener;

    @Before
     public void setUp() {
         instance = new ServiceControllerImpl();
         dataChangeListener = (IDataChanged) dataChangeListener;
         // instance and dataChangeListener "look like" two different object.
     }
}

I prefer second solution because in reality, maybe in future we can change the interface it implements to other objects, so force using concreted class maybe leads to fail test in the future. I don't know best practice for this problem.

Thanks :)

Aucun commentaire:

Enregistrer un commentaire