2012-04-02 12 views
9

में प्रॉपर्टी लोडिंग फ़ाइल मैं अपने जेनिट परीक्षण निष्पादन के दौरान अपने क्लासपाथ से sample.properties लोड करने का प्रयास कर रहा हूं और यह कक्षा पथ में फ़ाइल नहीं ढूंढ सकता है। अगर मैं जावा मेन क्लास लिखता हूं तो मैं फ़ाइल को ठीक से लोड करने में सक्षम हूं। मैं अपने जुनीट को निष्पादित करने के लिए नीचे के चींटी कार्य का उपयोग कर रहा हूं।JUnit @BeforeClass

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> 

आउटपुट:

[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] 

उत्तर

9

आप compile.classpath को ${build.classes.dir} जोड़ने की जरूरत है।

अद्यतन: टिप्पणियों में संचार के आधार पर, यह classpath समस्या नहीं थी। इसके बजाए गलत क्लासलोडर का इस्तेमाल किया गया था।

Class.getReasourceAsStream() क्लासलोडर के आधार पर संसाधन का मार्ग देखता है जिसे वर्ग लोड किया गया था। चूंकि यह Properties वर्ग को Testing वर्ग की तुलना में एक अलग क्लासलोडर द्वारा लोड किया गया था, और संसाधन पथ उस क्लासलोडर के क्लासपाथ के संबंध में गलत था। समाधान के बजाय Testing.class.getReasourceAsStream(...) का उपयोग करना था।

+0

प्रतिक्रिया के लिए धन्यवाद। मैंने compile.classpath जोड़ा जिसमें $ {build.classes.dir} शामिल है जो उपरोक्त निर्माण/वर्गों को हल करने का संकल्प करता है क्योंकि यह आपके द्वारा सुझाए गए पहले से ही था, इसलिए दुर्भाग्यवश यह मेरी समस्या नहीं है। – c12

+0

यह एकमात्र अन्य समय हो सकता है (AFAIK) तब होता है जब आप किसी वर्ग से संसाधन लोड करने का प्रयास कर रहे होते हैं जो कि आपकी कक्षा से अलग वर्ग-लोडर द्वारा लोड किया गया था। 'GetClass()। GetReourceourcessStream (...)' 'prop.getClass() के बजाय 'getResourceAsStream (...)'। अगर यह आपकी समस्या को हल करता है तो मुझे बताएं और मैं जवाब – Attila

+0

इनपुट अपडेट करूँगा = testing.class.getClassLoader()। GetResourceAsStream ("sample.properties"); काम किया, सुझाव के लिए धन्यवाद। – c12