Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37300

Re: Testing the functionality of sub classes

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Testing the functionality of sub classes
Date 2017-08-19 16:47 -0400
Organization A noiseless patient Spider
Message-ID <ona7tq$igi$1@dont-email.me> (permalink)
References <87fucnh2wu.fsf@munus.decebal.nl> <subclasses-20170819193036@ram.dialup.fu-berlin.de> <87y3qffim5.fsf@munus.decebal.nl>

Show all headers | View raw


On 8/19/2017 3:44 PM, Cecil Westerhof wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
> 
>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>> I have a class that is extended in several sub classes. Is there a way
>>> to let the tests for the super class to be run on all sub classes?
>>
>>    You are looking for all subclasses of a class.
>>
>>    Maybe you can create the JavaDoc and then evaluate
>>    the »All Know Subclasses« clause of the documentation
>>    of this class?
>>
>>    Another way to do it without JavaDoc:
>>
>>    I assume we have JARs with the class and the subclasses.
>>
>>    We can iterate all entries of a JAR file.
> 
> I should have been more clear.
> 
> For the test of the super class AbstractSQL, I have written a test
> class SQLTest in which I implemented all the tests with the sub class
> SQLite3. But there will be more. At the moment there are only two sub
> classes (the other is SQLH2) and for two classes it is not to much
> work. But I expect it to become a little more. So I would like to
> write the test class so that all the tests are done for all the sub
> classes.

     Much depends on the way the tests are written.  I confess I have
written few unit tests for abstract classes, but will offer a couple
thoughts anyhow -- take 'em or leave 'em, they may or may not be
worth what you're paying ...

     Thought #1: Since you can't instantiate an abstract class, the
only "in isolation" unit tests you can write must be for the static
methods, if there are any.  Testing those should be straightforward,
and isn't something you need to repeat for subclasses.

     Thought #2: To test the instance methods provided by the abstract
class, you need one or more concrete subclasses.  Instead of using a
"real" subclass (as you seem to be doing now), you might be better
off writing purpose-built subclasses whose only role is to exercise
the superclass' constructors and instance methods.  Note that such a
subclass need not be a complete implementation of everything that
would go into a "real" class, but can be a skeletal implementation
that provides only enough for its own particular test.  For example,
to test that your superclass constructor validates arguments as it
should and throws the right exceptions when they're wrong, all you
need is a subclass that invokes the constructor in several different
ways, some correct and some erroneous.

     Thought #3: Once testing has made you confident of the abstract
superclass, you need not re-test the superclass methods for every
"real" subclass.  Just write subclass-specific tests for the features
that are unique to the subclass.

     Thought #4: If the tests for #3 become repetitive, perhaps you
can write many of them to accept an instance of the subclass and
run tests on it (or maybe an instance factory, if the tests need
several instances):

	class CommonTests {
	    public static void runTest(Superclass instance) {
	        // ... test the subclass in its Superclass guise
	    }
	}

	class Subclass1Tests {
	    public static void runTests() {
	        CommonTests.runTest(new Subclass1());
	        // ... Subclass1-specific tests ...
	    }
	}

	class Subclass2Tests {
	    public static void runTests() {
	        CommonTests.runTest(new Subclass2(true));
	        CommonTests.runTest(new Subclass2(false));
	        // ... Subclass2-specific tests ...
	    }
	}

     Thought #5: If the tests for #4 turn out to have a lot of stuff
in the subclass-specific portions, ponder whether the abstract
superclass truly adds much value to the overall design.

-- 
esosman@comcast-dot-net.invalid
Twelve hundred fifty days to go.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 19:40 +0200
  Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 21:44 +0200
    Re: Testing the functionality of sub classes Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-19 16:47 -0400
      Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 01:30 +0200
        Re: Testing the functionality of sub classes Andreas Leitgeb <avl@logic.at> - 2017-08-20 11:50 +0000
          Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 14:49 +0200
            Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 15:36 +0200
              Re: Testing the functionality of sub classes Arne Vajhøj <arne@vajhoej.dk> - 2017-08-20 09:46 -0400
                Re: Testing the functionality of sub classes Andreas Leitgeb <avl@logic.at> - 2017-08-20 14:33 +0000
                Re: Testing the functionality of sub classes Arne Vajhøj <arne@vajhoej.dk> - 2017-08-20 11:14 -0400
                Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 16:35 +0200
                Re: Testing the functionality of sub classes Arne Vajhøj <arne@vajhoej.dk> - 2017-08-20 11:17 -0400
                Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 18:13 +0200
                Re: Testing the functionality of sub classes Arne Vajhøj <arne@vajhoej.dk> - 2017-08-20 12:28 -0400
                Re: Testing the functionality of sub classes Arne Vajhøj <arne@vajhoej.dk> - 2017-08-20 13:06 -0400
                Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 19:07 +0200
            Re: Testing the functionality of sub classes Arne Vajhøj <arne@vajhoej.dk> - 2017-08-20 11:21 -0400
              Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 18:17 +0200
          Re: Testing the functionality of sub classes v_borchert@despammed.com (Volker Borchert) - 2017-08-21 19:53 +0000
    Re: Testing the functionality of sub classes v_borchert@despammed.com (Volker Borchert) - 2017-08-19 21:15 +0000
      Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 01:36 +0200
    Re: Testing the functionality of sub classes Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 01:22 +0200

csiph-web