Dynamically loading and executing junit testcase from plugin [message #259238] |
Thu, 26 March 2009 09:05 |
Eclipse User |
|
|
|
Originally posted by: marius.eich.gmx.de
Hello everybody,
I have a problem concerning class loading (at least that's what I'm
suspecting). I'm writing a plugin that creates a JUnit 3 testcase and the
tested class in a temporary project inside the workspace. I am then trying
to execute the test via JUnitCore.runClasses(Class<?>...classes). Of course
I'll have to load my testclass first. I used the URLClassLoader and I'm
guessing that this is wrong, but don't know how to load the classes
otherwise.
I am able to load the needed testcase (without ClassNotFoundException) but
the test always fails because of "No runnable method Exception" as if the
test contained no test methods. I tried exactly the same thing with the same
testcase from a classic java project and it worked. So here's a code
fragment showing what I'm trying to do:
// run the testcase (load test class first)
URL[] urls = getClassLoadingArgs();
URLClassLoader classLoader = URLClassLoader.newInstance(urls);
Class<?> testCaseClass = classLoader.loadClass(packageName + "." +
testCaseName);
Result testResult = JUnitCore.runClasses(testCaseClass);
boolean testOk = testResult.wasSuccessful();
Can anybody tell me how to solve this problem?
Thanks in advance and best regards
Marius Eich
|
|
|
|
Re: Dynamically loading and executing junit testcase from plugin [message #259263 is a reply to message #259252] |
Fri, 27 March 2009 10:28 |
Eclipse User |
|
|
|
Originally posted by: marius.eich.gmx.de
Hi Walter,
thank you for your fast reply. I read the thread you mentioned and I got the
point that launching the test within the eclipse JVMcan be dangerous.
Now my problem is that I can't find any information about launching a JUnit
testcase within a new JVM (only about launching Java applications). I don't
really understand the usage of TestRunListener. After having registered a
listener with JUnitCore, how do I execute my testcase in a manner that the
listener is called? I would really appreciate if you could give me a hint
how to do that or where to find an example for it (you mentioned eclipse
launching a new JVM for JUnit tests).
Thanks in advance and best regards
Marius Eich
"Walter Harley" <eclipse@cafewalter.com> schrieb im Newsbeitrag
news:gqgmre$p88$1@build.eclipse.org...
> "Marius Eich" <marius.eich@gmx.de> wrote in message
> news:gqfggk$opk$1@build.eclipse.org...
>> Hello everybody,
>>
>> I have a problem concerning class loading (at least that's what I'm
>> suspecting). I'm writing a plugin that creates a JUnit 3 testcase and the
>> tested class in a temporary project inside the workspace. I am then
>> trying to execute the test via JUnitCore.runClasses(Class<?>...classes).
>> Of course I'll have to load my testclass first. I used the URLClassLoader
>> and I'm guessing that this is wrong, but don't know how to load the
>> classes otherwise.
>>
>> I am able to load the needed testcase (without ClassNotFoundException)
>> but the test always fails because of "No runnable method Exception" as if
>> the test contained no test methods. I tried exactly the same thing with
>> the same testcase from a classic java project and it worked. So here's a
>> code fragment showing what I'm trying to do:
>>
>>
>> // run the testcase (load test class first)
>>
>> URL[] urls = getClassLoadingArgs();
>> URLClassLoader classLoader = URLClassLoader.newInstance(urls);
>>
>> Class<?> testCaseClass = classLoader.loadClass(packageName + "." +
>> testCaseName);
>>
>> Result testResult = JUnitCore.runClasses(testCaseClass);
>>
>> boolean testOk = testResult.wasSuccessful();
>>
>>
>> Can anybody tell me how to solve this problem?
>
>
> I would really recommend launching a separate VM to run your test cases
> in, just as Eclipse itself does when executing JUnit tests. Otherwise you
> are loading user code into Eclipse's VM. There was a thread on this here
> just recently and I don't want to repeat it, but the bottom line is there
> are lots of good reasons to avoid that. (For instance, what if the code
> being tested changes a static field of some class that Eclipse relies on?)
>
> If you launch a separate VM, then of course you can put whatever you want
> to on its classpath.
>
|
|
|
|
Powered by
FUDForum. Page generated in 0.03811 seconds