Testing Method Signature

Today I had some new interesting testing task. What I needed was to ensure that some method has public and non-static signature. The method plays role of hook in OPML import utility class – the hook, which is used to create Reader object for a given URL. By default, it uses simple InputStreamReader wrapper, but the client application may wish to have something more sophisticated. So, the task was clear – to ensure that this method does not lose its signature, because it already happened once when I was reviewing the code and noticed that this method had unjustified public modifier. That time I changed it to “private static” and the application using this library lost its encoding detection skills.

Today, when I was looking for a bug in encoding detection it was a sort of revelation to see that the code even doesn’t get called. Yeah, sort of blown off the entire leg instead of simply shooting off the boots. So, traditionally I fixed the signature and put some warning for the future, but… the next thought was how to make an automated test to be one hundred per cent sure about it. It appeared simple and I would like to share it with you just to encourage.

public class ImporterTest extends TestCase
  // lots of other stuff skipped ...

  public void testCreateReaderForURLDefinition()
    throws NoSuchMethodException
    Method crfu = Importer.class.getMethod(
      "createReaderForURL", new Class[] { URL.class });

    assertEquals("Method should be public and non-static.",
      Modifier.PUBLIC, crfu.getModifiers());

Easy and bulletproof.