Hi,
I'm developing TestNG (5.10) support for PowerMock and I've run into
some issues. What I what to do is to add support for automatically
creating mocks and injecting them before each test method is executed
without forcing the user to declare anything in the TestNG xml file
(expect for the PowerMockObjectFactory class).
My first attempt was to programmatically add a listener from my
ObjectFactory but I couldn't find a way to do that (please point me in
the right direction if this is possible). So as a work-around I
created a proxy using Javassist of the test class and before each test
or "before" method invocation I'm creating and injecting the mocks.
This works fine when with standard test methods but when TestNG
executes the before method I get an exception IllegalArgumentException
saying that "object is not an instance of declaring class".
The reason is that the "thisMethod" argument in
MethodHelper#invokeMethod(..) points to the original test class method
in case of before methods (in my case it points to public void
samples.testng.AnnotationDemoWithBeforeMethodTest.setup()" but it
points to the proxied method in case of test method invocations (in my
case public final void samples.testng.AnnotationDemoTest_$
$_javassist_0.assertInjectionWorked() throws java.lang.Exception). So
there seems to be a difference in the way that TestNG looks up the
method to execute using reflection when it comes to test methods and
before methods. Why is this? And more importantly, how can I solve my
issues?
The code is available in the PowerMock svn repo at
http://powermock.googlecode.com/svn/trunk/modules/module-impl/testng
and the test case (which is current not included in the suite.xml
because it fails the build) is available at
http://code.google.com/p/powermock/source/browse/trunk/modules/module....
/Johan