On Mon, Mar 14, 2011 at 1:29 AM, Miroslav Pokorny <
[email protected]> wrote:

> The one thing I always find troublesome with annotation use as a config
> mechanism is they are unmodufiable. Most frameworks that use annotations
> almost never include an alternate way to programatically build the same
> config on that sub-system.


Most frameworks, except...  TestNG!

I identified this as a useful feature to have pretty early on, so TestNG has
so-called IAnnotationTransformers:

public interface IAnnotationTransformer {

  /**
   * This method will be invoked by TestNG to give you a chance
   * to modify a TestNG annotation read from your test classes.
   * You can change the values you need by calling any of the
   * setters on the ITest interface.
   *
   * Note that only one of the three parameters testClass,
   * testConstructor and testMethod will be non-null.
   *
   * @param annotation The annotation that was read from your
   * test class.
   * @param testClass If the annotation was found on a class, this
   * parameter represents this class (null otherwise).
   * @param testConstructor If the annotation was found on a constructor,
   * this parameter represents this constructor (null otherwise).
   * @param testMethod If the annotation was found on a method,
   * this parameter represents this method (null otherwise).
   */
  public void transform(ITest annotation, Class testClass,
      Constructor testConstructor, Method testMethod);
}


If you declare such a transformer, TestNG will hand you a modifiable version
of each annotation it encounters (@Test -> ITest, @BeforeMethod ->
IBeforeMethod, etc...), wait for you to modify it and then use that modified
version instead of the annotation found in the source.

This has turned out to be extremely powerful, in particular to apply
system-wide annotation modifications: for example automatically putting in a
group all the test methods that obey a certain criterion (e.g. belong to
package "foo" or have "Database" in their name) or adding a timeOut to
hundreds of test methods in one fell swoop.

Documentation 
here<http://testng.org/doc/documentation-main.html#annotationtransformers>
.

I wish more frameworks offered a similar facility.

-- 
Cédric

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to