JUnit: Generic interface testing

JUnit: Generic interface testing

Post by Patrick Roeme » Mon, 17 Apr 2000 04:00:00



Hallo,

while playing around with the JUnit package I've run into
problems defining generic Tests for all classes implementing
a generic interface.

Example: Given a generic codec interface with two methods,
encode() and decode(), I would like to write a generic test
that can be applied to all codec classes and that tests
whether X=decode(encode(X)). Of course it should be
extendable to specific tests for the respective codec
implementations.

At the moment I'm handling such cases like this (simplified
code):

--- <code> ---
abstract class CodecTest
{
   private CodecTestFactory factory;
   private Codec codec;

   public GenericTest(String name,CodecTestFactory factory)
   {
      super(name);
      this.factory=factory;
   }

   protected void setUp()
   {
      codec=factory.createCodec();
   }

   public void encDecTest()
   {
      // some test utilizing the codec instance
   }

   public static TestSuite createSuite(CodecTestFactory
factory)
   {
      TestSuite suite=new TestSuite();
      suite.addTest
            (
               new GenericTest("EncDecTest",factory)
               {
                  // (...)
               }
             );
      return suite;
   }

Quote:}

interface CodecTestFactory
{
   public Codec createCodec();

Quote:}

class SomeCodecTestFactory implements CodecTestFactory
{
   public Codec createCodec()
   {
      return new SomeCodec();
   }

Quote:}

class SomeCodecTest extends TestCase
{
   public static Test suite()
   {
      TestSuite suite=CodecTest.createSuite(new
SomeCodecTestFactory());
      // add any specific tests for SomeCodec
      return suite;
   }
Quote:}

--- </code> ---  

This works somehow, but it is rather ugly and I'm quite sure
it doesn't match the design intentions behind JUnit at all.

Does anybody have an idea what a more reasonable
implementation could look like?

Thanks in advance,
Patrick

 
 
 

JUnit: Generic interface testing

Post by St├ęphane Baillie » Mon, 17 Apr 2000 04:00:00




Quote:> Hallo,

> while playing around with the JUnit package I've run into
> problems defining generic Tests for all classes implementing
> a generic interface.

> Example: Given a generic codec interface with two methods,
> encode() and decode(), I would like to write a generic test
> that can be applied to all codec classes and that tests
> whether X=decode(encode(X)). Of course it should be
> extendable to specific tests for the respective codec
> implementations.

> At the moment I'm handling such cases like this (simplified
> code):

[...code...]

I'm sorry, I tried during a couple of minutes but I don't realy understand
your intention in your code.
You are defining an abstract CodecTest class, with a GenericTest constructor
I have no idea what is the name intent in your GenericTest constructor since
I have no idea what is the superclass.
I have no idea when you call your methods setUp or encDecTest().
I can understand part of your use of the abstract factory pattern, but it is
absolutely unclear.

I guess half of your intent is missing therefore I can not catch the purpose
of this mumbo jambo

Please repost a cleaner and detailed version, and please try to add at least
a couple of comments, do not forget that we, poor reader here, do not have
the full code to get the idea.

--
Stphane Bailliez, Paris - France


 
 
 

JUnit: Generic interface testing

Post by Frank Westpha » Mon, 17 Apr 2000 04:00:00



> while playing around with the JUnit package I've run into
> problems defining generic Tests for all classes implementing
> a generic interface.

> Example: Given a generic codec interface with two methods,
> encode() and decode(), I would like to write a generic test
> that can be applied to all codec classes and that tests
> whether X=decode(encode(X)). Of course it should be
> extendable to specific tests for the respective codec
> implementations.

This works fine for abstract base classes and interfaces that
adhere to the Liskov substitution principle:

  abstract class CodecTest extends TestCase
  {
    protected Codec codec;

    public CodecTest(String name)
    {
      super(name);
    }

    public void testEncode()
    {
      // do the actual testing
    }
  }

  class SomeCodecTest extends CodecTest
  {
    protected void setUp() {
      codec = new SomeCodec();
    }
  }

Frank

 
 
 

JUnit: Generic interface testing

Post by Patrick Roeme » Tue, 18 Apr 2000 04:00:00


Hi,

"Stphane Bailliez" schrieb:

Quote:> I'm sorry, I tried during a couple of minutes but I don't realy understand
> your intention in your code.
> You are defining an abstract CodecTest class, with a GenericTest constructor
> I have no idea what is the name intent in your GenericTest constructor since
> I have no idea what is the superclass.
> I have no idea when you call your methods setUp or encDecTest().
> I can understand part of your use of the abstract factory pattern, but it is
> absolutely unclear.

Sorry, I've posted broken code. GenericTest should be the
same as CodecTest.

Quote:> I guess half of your intent is missing therefore I can not catch the purpose
> of this mumbo jambo

The purpose of this code was to make an instance of the
specific implementation class to be tested part of the
fixture. And the reason for my posting was that I suspected
this code to be mumbo jambo in the third degree. :)

However, the approach suggested by Frank is absolutely
sufficient for what I'm trying to achieve. I've just
complicated the matter far beyond necessity.

Thanks,
Patrick