You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by Leo Simons <le...@apache.org> on 2004/01/12 00:09:03 UTC

[framework] let's get 100% test coverage!

Avalon-Framework is 4 years old, give or take; it's about time we give 
it some serious unit testing. I've already fixed three minor bugs in 10 
minutes of test writing, and I haven't even started testing the more 
complex code yet.

Who's game?

Unit Testing with Clover quick Howto
------------------------------------
For those wondering how to figure this out, its real simple. Get maven, 
and check out the avalon cvs module. The commands below assume linux, 
but the windows commands are nearly identical of course.

   cd avalon/framework/api
   maven clover; maven clover # you need to run it twice due to a maven+
                              # clover bug
   # take a look at the coverage report:
   # avalon/framework/api/target/docs/clover/index.html

If you see "red" in the coverage report, that's a problem. Pick one of 
the red bars, and look at the report in detail. As an example, 
ConfigurationException has 0% coverage. If you navigate the report for 
ConfigurationException, you find that (for example)

76 0 public ConfigurationException( final Configuration config )
77   {
78 0   this( "Bad configuration: " + config.toString(), config );
79   }

the first column are the line numbers, the second column shows how often 
a line of code was executed during the running of the test suite. The 
numbers will have a blue background if they're properly tested, and a 
red background if not.

Now, create a new test case in the appropriate location inside

   avalon/framework/api/src/test

In the ConfigurationException test case, we might start like this:

   public class ConfigurationExceptionTestCase extends TestCase
   {
     public void testConstructor()
     {
       assertNotNull(
         new ConfigurationException( new MockConfiguration() ) );

       try
       {
         new ConfigurationException( null );
         fail( "Expected a NullPointerException for a null argument!" );
       }
       catch( NullPointerException npe )
       {}
     }

     public class MockConfiguration implements Configuration
     {
       /** stub methods go here */
     }
   }

using an IDE here makes life a lot simpler, as it can (for example) 
autogenerate the body for the MockConfiguration class. Run the test in 
your IDE, and if it passes, rerun clover, and take a look at the updated 
report. The gutter for lines 76 and 78 will now have a blue background, 
and the code coverage bar for ConfigurationException will start showing 
a little green.

Simple keep repeating this process until all bars are completely green 
and all gutters for all lines have a blue background. Then start doing 
the same for the impl package.

It's also a good idea to take a look at how experienced test writers 
structure their tests. Take a look at the tests for QDox and 
PicoContainer for example. Those are structured quite nicely.

-- 
cheers,

- LSD

-----------------------------------------------------------------------
Weblog              -- http://leosimons.com/
IoC Component Glue  -- http://jicarilla.org/
Articles & Opinions -- http://articles.leosimons.com/
-----------------------------------------------------------------------
"We started off trying to set up a small anarchist community, but
  people wouldn't obey the rules."
                                                         -- Alan Bennett



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@avalon.apache.org
For additional commands, e-mail: dev-help@avalon.apache.org