2

I'm trying to run some tests that need values from a .properties file I'm using in production. I can verify the test is getting the location of the properties file via -DapplicationProperties="fname", however it's not being parsed. I'm also using spring, is it spring that normally parses this for me? If not, should junit be doing this automatically? If not, what's the best way to ensure the properties are available to all my tests via System.getProperty()?

Josh
  • 801
  • 3
  • 11
  • 16

2 Answers2

1

Neither Spring nor JUnit parses propertie files only because there is a parameter.

To load properties files in Spring the easyses way would be using a org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

See Spring Reference: 3.8.2.1 Example: the PropertyPlaceholderConfigurer an example.

See this blog for a way to distingush beween different kind of properties and there way to store: http://www.summa-tech.com/blog/2009/04/20/6-tips-for-managing-property-files-with-spring/

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • hmm, could this be what's parsing it? `` – Josh Jan 26 '12 at 21:08
  • @Josh I do not understand your comment! – Ralph Jan 26 '12 at 21:40
  • Sorry, I have the above line in my `dispatcher-servlet.xml` file. Could that be what's causing the properties file to be parsed by spring? – Josh Jan 26 '12 at 22:02
  • no - it search for Classes annotated with `@Respository`, `@Service`, `@Controller`, `@Component` in com.stuffs.controller and subpackages and instantiate that. – Ralph Jan 27 '12 at 06:50
  • then I'm not sure how the properties file gets parsed. Like I said we're passing the argument `-DapplicationProperties=path/here/something.properties` to the jvm, and I can confirm that. I'll keep looking... – Josh Jan 27 '12 at 14:03
  • well I'm not sure where that VM arg is being used.. but I'm trying to use it in my test to load the properties file, somewhat like this: http://stackoverflow.com/questions/6296539/issue-loading-properties-file or this: http://stackoverflow.com/questions/4777252/java-properties-file-not-loading .. but I'm still getting `null` when I try to get a property that I know exists in the file. – Josh Jan 27 '12 at 20:15
0

I created a library called System Rules for testing code, that uses java.lang.System. Using this library you write JUnit tests like these:

import static org.junit.Assert.assertEquals;
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
import org.junit.Rule;
import org.junit.Test;

public void MyTest {
  @Rule
  public ProvideSystemProperty myPropertyHasMyValue
    = new ProvideSystemProperty("MyProperty", "MyValue");

  @Test
  public void propertyIsThere() {
    assertEquals("MyValue", System.getProperty("MyProperty"));
  }
}

It's possible to use properties from properties files, too:

@Rule
public ProvideSystemProperty properties
 = ProvideSystemProperty.fromFile("/home/myself/example.properties");

or

@Rule
public ProvideSystemProperty properties
 = ProvideSystemProperty.fromResource("example.properties");
Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
  • I'd prefer to use the properties file that was used in production, in case that changes in the future – Josh Jan 26 '12 at 21:33
  • System Rules 1.1.0 supports properties files. – Stefan Birkner Feb 01 '12 at 11:18
  • so (from the two newest example), I could use `System.getProperty("somethingFromTheFile");` after loading the file? That's the goal. Oracle's UCP needs the property, so it has to be available via `System.getProperty` (afaik). – Josh Feb 10 '12 at 16:14
  • Yes, you can use the property from the file within your test. – Stefan Birkner Feb 11 '12 at 21:11