Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Dynamically loading and executing junit testcase from plugin
Dynamically loading and executing junit testcase from plugin [message #259238] Thu, 26 March 2009 09:05 Go to next message
Eclipse UserFriend
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 #259252 is a reply to message #259238] Thu, 26 March 2009 19:59 Go to previous messageGo to next message
Walter Harley is currently offline Walter HarleyFriend
Messages: 847
Registered: July 2009
Senior Member
"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.
Re: Dynamically loading and executing junit testcase from plugin [message #259263 is a reply to message #259252] Fri, 27 March 2009 10:28 Go to previous messageGo to next message
Eclipse UserFriend
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.
>
Re: Dynamically loading and executing junit testcase from plugin [message #259272 is a reply to message #259263] Sat, 28 March 2009 05:04 Go to previous message
Walter Harley is currently offline Walter HarleyFriend
Messages: 847
Registered: July 2009
Senior Member
"Marius Eich" <marius.eich@gmx.de> wrote in message
news:gqi9ot$d36$1@build.eclipse.org...
> 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

Hm, I could make something up but the person who would really be able to
give us a definitive answer is Darin Wright, the JDT debug lead. I've
pinged him and hopefully he'll have time to contribute an answer. I think
he's been at EclipseCon this week so we might have to wait till next week
for him to get back.
Previous Topic:Code Assist on Method Calls - How to get the target variable?
Next Topic:Can one selectively format java code?
Goto Forum:
  


Current Time: Tue Jul 16 15:43:03 GMT 2024

Powered by FUDForum. Page generated in 0.04808 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top