Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Properties File In JUnit @BeforeClass

Tags:

java

junit

ant

I'm trying to load sample.properties from my classpath during my JUnit test execution and it can't find the file in the class path. If I write a Java Main class I'm able to load the file just fine. I'm using the below ant task to execute my JUnit.

public class Testing {
 @BeforeClass
    public static void setUpBeforeClass() throws Exception {   
        Properties props = new Properties();
        InputStream fileIn = props_.getClass().getResourceAsStream("/sample.properties");
        **props.load(fileIn);**
    }

}

JUnit:

<path id="compile.classpath">
        <pathelement location="${build.classes.dir}"/>
    </path>
    <target name="test" depends="compile">
            <junit haltonfailure="true">
                <classpath refid="compile.classpath"/>
                <formatter type="plain" usefile="false"/>
                <test name="${test.suite}"/>
            </junit>
        </target>
        <target name="compile">
            <javac srcdir="${src.dir}" 
                   includeantruntime="false"
                   destdir="${build.classes.dir}" debug="true" debuglevel="lines,vars,source">
                <classpath refid="compile.classpath"/>
            </javac>
            <copy todir="${build.classes.dir}">
                <fileset dir="${src.dir}/resources"
                         includes="**/*.sql,**/*.properties" />
            </copy>
        </target>

Output:

[junit] Tests run: 0, Failures: 0, Errors: 1, Time elapsed: 0.104 sec
[junit] 
[junit] Testcase: com.example.tests.Testing took 0 sec
[junit]     Caused an ERROR
[junit] null
[junit] java.lang.NullPointerException
[junit]     at java.util.Properties$LineReader.readLine(Properties.java:418)
[junit]     at java.util.Properties.load0(Properties.java:337)
[junit]     at java.util.Properties.load(Properties.java:325)
[junit]     at com.example.tests.Testing.setUpBeforeClass(Testing.java:48)
[junit]
like image 389
c12 Avatar asked Apr 02 '12 20:04

c12


People also ask

What is the purpose of the @BeforeClass annotation in JUnit?

org.junit Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those of the current class, unless they are shadowed in the current class.

What does @before mean in JUnit?

In JUnit 5, the tags @BeforeEach and @BeforeAll are the equivalents of @Before and @BeforeClass in JUnit 4. Their names are a bit more indicative of when they run, loosely interpreted: 'before each tests' and 'once before all tests'.


2 Answers

This works:

YourTestClass.class.getClassLoader().getResourceAsStream

This doesn't work:

YourTestClass.class.getResourceAsStream

like image 196
ACV Avatar answered Sep 21 '22 17:09

ACV


You need to add ${build.classes.dir} to compile.classpath.

Update: Based on communication in the comments, it turned out the classpath was not the problem. Instead the wrong class loader was used.

Class.getResourceAsStream() looks up the path of the resource based on the class loader the class was loaded by. As it turns out the Properties class was loaded by a different class loader than the Testing class, and the resource path was incorrect with relation to that class loader's classpath. The solution was to use Testing.class.getResourceAsStream(...) instead of Properties.class.getResourceAsStream(...).

like image 41
Attila Avatar answered Sep 19 '22 17:09

Attila