transMorphic
Mike Roberts' blog on tech and life














Subscribe to "transMorphic" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.
 

 

18 August 2002
 

From [Joe's Jelly]

Quick Tip: Keeping unit tests in parallel source trees

It's common practise to bundle unit-tests in the same source tree as production code. To distinguish between test code and production code, tests are placed in a subpackage called test.

<snip>

However, if tests are kept in parallel source trees and the test subpackage is ditched, it gives some useful advantages:

<snip>

[Joe's Jelly]

My opinion is keep them in sub-packages in the same tree. Even if you have your test classes in the same package, you still can't access private methods / fields, which are sometimes worth testing (especially if you have an algorithm in a private method). Besides which, having 2 source trees is a pain in the nuts (I can't think immediately of a quick way of doing it nicely in IDEA), and it really isn't too hard to tell Ant to not bother with test classes for production builds or JavaDocs (sorry Joe! :) )

To get around this, I've been using reflection to get at private members (which works if your Security Manager lets you, which it does by default), which is totally legitimate at test-time (even though at production run-time you'd probably want to do something more reasonable).

You can have a class which has a set of utility methods to let you do such things and, for example, you could use the following to get at normally inacessable methods (credit to Mike Royle, fellow ThoughtWorker, for this):

public static Object callInacessableMethod(Object targetObject, String methodName, 
Class[] parameterTypes, Object[] parameterValues)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
  Method method = getMethodByName(targetObject.getClass(), methodName, parameterTypes);
  method.setAccessible(true);
  return method.invoke(targetObject, parameterValues);
}
private static Method getMethodByName(Class target, String name, Class[] parameterTypes) 
throws NoSuchMethodException{
  try {
   return target.getDeclaredMethod(name, parameterTypes);
  } catch (NoSuchMethodException e) {
   if (target.equals(Object.class))
    throw e;
   return getMethodByName(target.getSuperclass(), name, parameterTypes);
  }
}


In the meantime, I'm going to go and figure out how to quote other people nicely in my blog...


9:35:26 PM    

So, what to put on here as a first contentful post? Hmm, a list of things that are on my mind, I guess...

  • I'm thinking of collecting some thoughts together on the subject of 'IT Ops Support for Agile Development Teams'. I've been on both sides of the Dev / Ops 'No-Mans Land', and I think there's a lot to think about in this area.
  • What my next computer purchase will be. Either an upgrade for my existing machine, a 17+" flat panel, an SS51, or maybe even a Mac *gasp*. Someone at work was showing me their widescreen Powerbook running OS X 10.1 and it was gorgeous.
  • Java / .Net web services
  • The best way to do XML serialization from Java / .Net objects for both persistence and remote transport.
  • What I'm going to read after I've finished Asimov's Foundation and then Orson Scott Card's Ender's Game
  • Whether a comment link will appear at the end of this post...

8:55:28 PM    


Click here to visit the Radio UserLand website. © Copyright 2003 Mike Roberts.
email : blog @ tmorph . com

Last update: 11/02/2003; 00:06:24.
This theme is based on the SoundWaves (blue) Manila theme.
August 2002
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Jul   Sep