How can we verify that a class implements many interfaces?

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

public class ServiceControllerImpl extends ServiceController implements IDataChanged, IEventChanged {

}

Now there are two ways to test. The first test is performed directly on a particular class. This means that the type of the object is a specific class, not an interface.

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

The second way is testing only on the interface. We must give this object to all the interfaces that it implements.

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

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

I prefer the second solution, because perhaps in the future we will be able to change the interface that it implements for other objects, so using a particular class may lead to unsuccessful tests in the future. I do not know the best practice for this problem.

Thank:)

+4
2

, , , , , , , .

, , , . : IDataChanged ServiceControllerImpl?

ServiceControllerImpl, , IDataChanged ServiceControllerImpl, , IDataChanged - . , .

, . A unit test . , . , , . , , .

, api - . , . .

public abstract class SetTest {

    @Test
    public void addAlreadyExistentObject(){
        Set<String> setUnderTest = createSetUnderTest();
        Assert.assertTrue(setUnderTest.isEmpty());

        boolean setChanged = setUnderTest.add("Hello");
        Assert.assertTrue(setChanged);

        setChanged = setUnderTest.add("Hello");
        Assert.assertFalse(setChanged);

        Assert.assertEquals(setUnderTest.size(), 1);

    }

    protected abstract Set<String> createSetUnderTest();

}

, api . .

public class HashSetTest extends SetTest {

    @Override
    protected Set<String> createSetUnderTest() {
        return new HashSet<String>();
    }
}

, .

api . Runnable s?

public class RunnableTest {

     @Test
     public void run(){
         Runnable runnable = ...; 

         // What to test here?
         // run is invoked without throwing any runtime exceptions?
         runnable.run();

     }
}

, , .

api, Set api, , , .

+1

JayC667 , , . , , :

public class ServiceControllerImplTest {
    ServiceController controller;
    IDataChanged dataChangeListener;

    @Before
     public void setUp() {
         instance = new ServiceControllerImpl();
         controller = instance;
         dataChangeListener = instance;
     }
}
+1

All Articles