aspose file tools*
The moose likes Testing and the fly likes Junit problem, how to load  a properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Junit problem, how to load  a properties file" Watch "Junit problem, how to load  a properties file" New topic
Author

Junit problem, how to load a properties file

Fernando Guerrero
Greenhorn

Joined: Apr 26, 2011
Posts: 14

Hello

I am just learning how to use JUnit , so me experience with this framework is very basic. At this point I am having a problem to test some java code. I have a java class that requires a properties file to run correctly, many methods in this file require information that is inside the properties file. In most of the properties I have a default value, but there are some properties that given their dynamic status it doesn't make sense put a default value.

Now, the problem that I have is that I don't know how to load or simulate this properties file to do my JUnit tests. Any suggestion ??? I use eclipse IDE or ant/linux command line to compile, test and run my applications. Most of my java application require properties file, so I really need to find a way to to a better testing.

Thanks in advance.
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Fernando,

At the risk of stating the obvious, JUnit is "just" a java process. So, however you specify what property file to use for your application should work the same way for the test(s) you are trying to work. As such, set up a property file with whatever options you want and ensure it is specified at exection time (dir in the classpath, or as a -D parm, etc depending on whether you are using a standard access method or a custom one).

If you are trying to use a different file for each test or for groups of tests there would be different approaches that could be used. I am sensing you are just asking about setting one up at this point?
Fernando Guerrero
Greenhorn

Joined: Apr 26, 2011
Posts: 14

Thank Brian

Basically I should be able to execute the JUnit test loading the properties file in the same way that I do it wit the java class to be tester, right ?
So If I run my java application in the next way:
#java -DPROPERTIES =/home/myPropertiesFile.props com.fgr.servers MyJavaApplication

I should be able to run the JUnit test in the next way
#java -DPROPERTIES =/home/myPropertiesFile.props org.junit.runner.JUnitCore MyJUnitTest
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Fernando,

At a glance, that's what I'd expect. Similar to that, any classpath entries on the original program need to be defined for the JUnit, etc.

Depending on how you are locading the props, you may even be able to specify the file in the test method itself using "System.setProperty" to set your props file. The -D is not used in this case - from your post using this: System.setProperty("PROPERTIES","/home/myPropertiesFile.props");
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10520
    
    9

Moving to a more suitable forum.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Junit problem, how to load a properties file