• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runnig test files with JUnit.core.run

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody, I have a little problem and I'm sure that you will solve it in a minute.
In my eclipse plug in - I let the user to choose java file with the fileChooser (it returns FILE)
afterwards I want to run the test with JUnit.core.run that receive Class parameter.
How do I get the Class from the java file I've chosen?
Thank you very much
hadas
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hadas,
If you read the file, the first non commented line is the package statement. And the classname is in the filename. Merging the two gives you a fully qualified class name.
 
hadas yaari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much
 
hadas yaari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne,
I tried your idea and still I have a problem in run time.
I use Class.forName() that gets as parameter the class name (string) and suppose to create a class from it.
But I get an exception: class not found.
I suppose that Class.forName() doesn't look in the right place for the class.
Is there a way to tell it where to look?
The classes (test cases) are part of the current project.
I thought maybe I should paste a prefix to the class name but I don't know what prefix.
The purpuse is to give JUnit.core.run() a class to run.
In a lot of examples that I saw the class is given directly:

I'm helpless - Can you please direct me?
Thank you very much
Hadsa
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hadsa,
The file object has a method called getAbsolutePath(). This will give you the full name. For example, c:/workspace/com/javaranch/MyClass.class. You'll need to derive the package name from this path. Since package names typically start with com/org/net, you should be able to parse the string to get it. Then just turn the slashes into dots, remove the file extension and you've got a fully qualified class name!
 
hadas yaari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne,
Sorry for being a little bit annoying but I think I didn't explain myself clearly.
We develop an eclipse plug-in.
A part of the plug-in is the option to run a unit tests from eclipse (when clicking on a button that is part of our plug-in).
The unit tests that the user would want to run are the unit tests of his project ( a project that he opened in eclipse that the plug in is installed in it).
When clicking on the button, the user can choose the wanted unit test files with a fileChooser and then when clicking "run tests", the tests should run by using JUnit.core.
When I run this plug in order to test and debug it, a new eclipse application( with the plug in installed in it) is opened and the user can work: open a java project, add classes, write unit tests etc.
When the user clicks on "run tests" I convert the file names into class names by replacing "/" with "." and removing the ".java" from the file name (as you suggested and as I did before). when I send this class name into Class.forName(), it searches the class in my project workspace (the project of the plug in) and not in the user's workspace (run time workspace) and that is the reason that the class is not found.
I want the possibility to run these unit tests in run-time from the user's workspace - this is the purpse of the plug-in: giving more services for the developer that uses the plug-in.
We want to run the unit tests like this in order to generate a special report.
Maybe I don't have to use Class.forName(). Maybe there is another way to load the class in order to give it to JUnit.core.run(). And of course the unit test have to run in the user's project environment because it tests the project classes.
Sorry for this long exhausting post.
I hope that now you understand the problem and maybe now you would be able to suggest me a solution.
Thank you very very much
Hadas
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh! You have an Eclipse plugin question. I'll move this to our Eclipse forum.
 
hadas yaari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very mucn, I appriciate your help.
Can you please put the Eclipse Forum link here - I can't find it.
Thank you very much and have a great weekend.
Hadas
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hadas yaari wrote:Thank you very mucn, I appriciate your help.
Can you please put the Eclipse Forum link here - I can't find it.
Thank you very much and have a great weekend.
Hadas


You are in it. The new software is subtle and provides links to the new location.

https://coderanch.com/forums/f-12/IDEs-Version-Control-other-tools
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zeca Pistolas,
Your post was moved to a new topic.
 
reply
    Bookmark Topic Watch Topic
  • New Topic