Unable to locate File on getResource function

classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to locate File on getResource function

ken pelayo
This post was updated on .
hello Tarun,


i tried to run datadriven test using your csv reader function and i got stuck in the code below:

FindAFlightData.class.getResource("/TestData/Resources/FlightTest/FlightData.csv").getPath()));

however in your code it is:
RegistrationData.class.getResource("/regData.csv").getPath()));


what is the exact string to place inside getResource()?

in my case i just copied the complete path of the file which is /TestData/Resources/FlightTest/FlightData.csv under the Project Mercury_tours that i created.


by the way i wanna ask about the properties function in the RegistrationData class what is its purpose?
do i need to include that on my data class?

public RegistrationData() throws IOException {
                properties.load(RegistrationData.class
                                .getResourceAsStream("/registrationdata.properties"));

thanks

Note: Edited the code above from FindAFlightData.class.getResource("/regData.csv").getPath())); to RegistrationData.class.getResource("/regData.csv").getPath())); (your original code)
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

softwaretestingforum
Administrator
Hi Ken,

I suppose you are referring to 7th Selenium Training Session which uses maven.
Maven should automatically pick up file from resource folder.
Could you post a snapshot of the project structure you have? Please expand all the folders in snapshot.
It would help me debug the issue.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
Hi Tarun,

unfortunately i did not use Maven on my structure. i just copied the csvreader code and customized it based on my implementation.

Please see image below for my project structure:


projectStructure
Thanks in adavance

Ken
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

softwaretestingforum
Administrator
Though image is not very clear, I get your point.
When using maven, it knows that it has to look for file in "resource" folder.
But since you are not using maven you would have to adopt another mechanism.
"getResourceAsStream" needs path to file but you can not provided it hard coded path because then test would not work in machine of other test engineers.

You could build path dynamically, in this case.
Consider I have test data file in following location -

"C:\Users\tbhadauria\seleniumtestworkspace\apitesting\testdata\jsonexample.json"
where "jsonexample.json" is my test data file.

Now to get the absolute path to file, I can use -

System.getProperty("user.dir")+"//testdata//jsonexample.json"
And then you can pass it to method - getResourceAsStream, so it becomes -

getResourceAsStream(System.getProperty("user.dir")+"//testdata//jsonexample.json")
Hope this helps.
 
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
I'll try this solution and update you of the results...

Thanks Tarun
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
i did apply your solution stated above but im having an error on the code

the code below:

CSVReader csvReader = new CSVReader(new FileReader(
                                FindAFlightData.class.getResourceAsStream(System.getProperty("user.dir")+"\\TestData\\Resources\\FlightTest\\FlightData.csv" )));

the error says: The constructor FileReader(Inputstream) is undefined.

What could be the error here.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

softwaretestingforum
Administrator
ken pelayo wrote
getResourceAsStream(System.getProperty("user.dir")+"\\TestData\\Resources\\FlightTest\\FlightData.csv" )));
There seem to be extra bracket. It should be

getResourceAsStream(System.getProperty("user.dir")+"//TestData//Resources//FlightTest//FlightData.csv");

Can you try this?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
I removed the brackets as suggested but i think it was not the problem. the problem still persists.

anyways i created a video capture of my structure to have clear view of my implementation

http://screencast.com/t/Yx2kAPVb

please see video on the link above.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

softwaretestingforum
Administrator
Could you export your project and attached here?
Right Click on Project > Export > Archive File
I would add jar files so you can ignore them in export.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
Hello Tarun,

i have created an archive file of the my structure. please see attached.

Thanks...FlightTest.zip

the specific code with the error is in FindAFlightData.class
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

softwaretestingforum
Administrator
Hi Ken,

I assume that you have CSV reader in file path.
The problem we had was that constructor FileReader was looking for file path while we passed stream to it.
I should have noticed it earlier.

Right code would be -

CSVReader csvReader = new CSVReader(new FileReader(
				FindAFlightData.class.getResource(System.getProperty("user.dir")+"//TestData//Resources//FlightTest//FlightData.csv" ).getPath()));

Here we pass path of file to FileReader constructor.
Hope this helps.
I would elaborate more on these topics once I begin with java training.

~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
Ill Try this and feedback as soon as i get the results.

java training is indeed helpful. ill be looking forward for that soon

Thanks...
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
Hi Tarun,

Caused by: java.lang.NullPointerException
        at _2DataObject.FindAFlightData.getCSVData(FindAFlightData.java:189)

i got the error above at the exact code you suggested.

i should be learning java in depth.
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

softwaretestingforum
Administrator
I can debug it but I am curious to know why you don't want to follow maven which makes it easy to read data from external resources
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
i'll try to review the maven session. i didn't clearly get the maven tutorial.

that's why i just followed my own structure in performing my test.

anyways thanks a lot....
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

softwaretestingforum
Administrator
hm in that case could you tell me what you tried with maven and where you found difficulties.
I would try to help
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
what i don't get about the tutorial is that how to create the project structure because you just copied the commands from notepad. i wasn't able to get how and why those commands were used.

and also in converting the current project to maven structure project. Thanks....
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

softwaretestingforum
Administrator
The command I used in tutorial is to create maven project template and then more packages, files etc could be added to it. I used maven archetype for it. Did you execute this command and got sample maven project?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Unable to locate File on getResource function

ken pelayo
i was able to create a sample project with the commands given on the tutorial.
and the challenge now is converting my current structure to maven. what i did was i converted it thru
right click on the project then configure-->convert to Maven Project. i was expecting that it will automatically be structured same as your example where folders are arranged in src\main\ (for main classes) and src\test (for test execution classes). but it still has the same structure as the previous project the only diff is it has "M" on the project folder.

do i still need it to structure the folders manually?to follow that convention?