Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Junit problem, how to load a properties file

 
Fernando Guerrero
Greenhorn
Posts: 15
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to a more suitable forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic