2012-10-05 13 views
6

मुझे एहसास है कि this one के रूप में बहुत सारे प्रश्न हैं, हालांकि उनमें से अधिकतर पढ़ने के बाद मुझे एक ही समस्या है, लेकिन विभिन्न कारणों से।चींटी जुनीट क्लास नॉटफाउंड एक्सेप्शन

मैं अपने एंटी बिल्ड के अंदर <junit> कार्य चला रहा हूं और ClassNotFoundException प्राप्त कर रहा हूं। समान रूप से संबंधित प्रश्नों में से अधिकांश (जैसे उपर्युक्त) में, यह लेखक की विभिन्न डिग्री के रूप में सामने आया, जो कि जुनीट क्लासपाथ को सही तरीके से कॉन्फ़िगर नहीं कर रहा है। यद्यपि यह मेरे लिए बहुत अच्छा हो सकता है, लेकिन इनमें से किसी भी अन्य प्रश्न के लिए कोई भी सुझाव मेरे लिए काम नहीं करता है।

मेरे परियोजना निर्देशिका संरचना:

MyProject/ 
    src/main/java/ 
     FakeObject.java 
     ...the rest of my Java main source 
    src/test/java 
     FakeObjectUnitTest.java 
     ...the rest of my Java test source 
    bin/main 
     FakeObject.class 
     ...the rest of main Java binaries 
    bin/test 
     FakeObjectUnitTest.class 
     ...the rest of main Java binaries 
    lib/main 
     ...my main dependencies 
    lib/test 
     junit-4.10.jar 
     hamcrest-1.1.jar 
     ...the rest of my test dependencies 
    gen/reports 
     ...where JUnit should be placing all test reports 
    build/ 
     build.xml 
     build.properties 

src/test/java/FakeObjectUnitTest.java में:

// Package and imports omitted 

public class FakeObjectUnitTest { 
    @Test 
    public void doSomething() { 
     int x = 5; 

     Assert.assertEquals(x, 5); 
    } 
} 

मेरे build.xml:

<project name="" basedir=".."> 
    <!-- Main classpath. --> 
    <path id="main.class.path"> 
     <fileset dir="bin/main"/> 
    </path> 

    <!-- Test classpath. --> 
    <path id="test.class.path"> 
     <fileset dir="bin/test"/> 
    </path> 

    <!-- JUnit classpath. --> 
    <path id="junit.class.path"> 
     <fileset dir="lib/main" includes="*.jar"/> 
     <fileset dir="lib/test" includes="*.jar"/> 
     <path refid="main.class.path"/> 
     <path refid="test.class.path"/> 
    </path> 

    <!-- 
     All previous targets omitted for brevity (such as for compiling, etc.), but 
    I assure you all that they work and compile FakeObject.java/FakeObjectUnitTest.java into 
    FakeObject.class/FakeObjectUnitTest.class respectively, and place them in the correct 
bin directories. 
    --> 

    <target name="run-junit" depends="compile"> 

     <property name="junit.path" refid="junit.class.path"/> 
     <echo message="JUnit ClassPath is: ${junit.path}"/> 

     <junit printsummary="yes" haltonerror="yes" haltonfailure="yes"> 
      <classpath refid="junit.class.path"/> 

      <formatter type="xml"/> 

      <batchtest todir="gen/reports"> 
       <fileset dir="src/test/java"> 
        <include name="**/*UnitTest*.java"/> 
       </fileset> 
      </batchtest> 
     </junit> 
    </target> 
</project> 

जब मैं कमांड लाइन से इस लक्ष्य को चलाने:

run-junit: 
    [echo] Conducting unit tests with JUnit. 
    [echo] JUnit ClassPath is: /<path-to-my-project>/MyProject/lib/test/hamcrest-1.1.jar:/<path-to-my-project>/MyProject/lib/test/junit-4.10.jar:/<path-to-my-project>/MyProject/bin/main/com/myproject/client/FakeObject.class:/<path-to-my-project>/MyProject/bin/test/com/myproject/client/FakeObjectUnitTest.class 
    [junit] Running com.myproject.client.FakeObjectUnitTest 
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 

मैं इसे साबित करता हूं कि यह जुनीट क्लासपाथ पर FakeObjectUnitTest.class देखता है। हालांकि, यह एक त्रुटि के साथ विफल रहता है। जब मैं इस परीक्षण के लिए एक्सएमएल परीक्षण रिपोर्ट में जाते हैं, मैं निम्न देखें:

<error message="com.myproject.client.FakeObjectUnitTest" type="java.lang.ClassNotFoundException">java.lang.ClassNotFoundException: com.myproject.client.FakeObjectUnitTest 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:247) 
</error> 

जब मैं वर्बोज़ मोड में चींटी चलाने (-v) केवल संकेत मैं देख रहा हूँ की तर्ज पर चेतावनी का एक समूह रहे हैं :

की उपेक्षा अपवाद java.util.zip.ZipException: उद्घाटन ज़िप फ़ाइल पढ़ने संसाधन एक्स

... जहां x एक वर्ग फ़ाइल का नाम है में त्रुटि।

मुझे this एसओ पर सवाल मिला, और समस्या यह साबित हुई कि लेखक ने क्लासपाथ में क्लास फाइलें जोड़ दी हैं, न कि जार (जो जुनीट को दुखी करता है)। तो मैंने junit.class/path से main.class.path और test.class.path को हटाने का प्रयास किया और फिर मुझे अपवाद मिला कि कक्षाओं में से कोई भी फ़ाइल नहीं मिल सकती है।

क्या कोई विचार या विचार क्या हो रहा है? अग्रिम धन्यवाद, और -v प्रश्न के लिए खेद है, लेकिन मैंने अधिक जानकारी, बेहतर बताया।

उत्तर

5

आप पैकेज जैसी संरचनाएं के लिए उपयोग करने की आवश्यकता:
From the manual:

जहाँ पथ की तरह मूल्यों की आवश्यकता निर्दिष्ट करने के लिए, एक नेस्टेड तत्व का उपयोग किया जा सकता है।

<classpath> 
     <pathelement path="${classpath}"/> 
     <pathelement location="lib/helper.jar"/> 
    </classpath> 

तो अपने समाधान दिखाई देगा:: रन JUnit लक्ष्य में

<!-- JUnit classpath. --> 
<path id="junit.class.path"> 
    <fileset dir="lib/main" includes="*.jar"/> 
    <fileset dir="lib/test" includes="*.jar"/> 
    <pathelement location="bin/main"/> 
    <pathelement location="bin/test"/> 
</path> 
+0

प्रति सामान्य, आप आदमी हैं। धन्यवाद! – IAmYourFaja

2

अपने classpath को देखते हुए:

...:/<path-to-my-project>/MyProject/bin/main/com/myproject/client/FakeObject.class:... 

आप उस तरह classpath में एक व्यक्ति वर्ग नहीं डाल सकते हैं।इसके बजाय, निर्देशिका classpath में अपने पैकेज पदानुक्रम के आधार पर है कि डाल:

...:/<path-to-my-project>/MyProject/bin/main:... 
+0

धन्यवाद @Jesper (यह सामान्य रूप ले लेता है +1) - लेकिन यदि आप ध्यान देते हैं, तो एकमात्र कारण है कि क्यों 'FakeObject.class' जुनीट क्लासपाथ में है क्योंकि यह एकमात्र बाइनरी होता है जो संकलित हो रहा है। यह क्लासपाथ में है क्योंकि यह 'बिन/मेन' के अंदर है, जो 'main.class.path' का हिस्सा है, जो बदले में' junit.class.path' का हिस्सा है, इसलिए जब तक कि मुझे कुछ याद नहीं आ रहा है, ' बिन/मुख्य' पहले से ही निर्माण है जो जुनीट क्लासपाथ का हिस्सा है। – IAmYourFaja

0

मेकअप संशोधन इस प्रकार,

  <batchtest todir="gen/reports"> 
      <fileset dir="bin/tests"> 
       <include name="**/*UnitTest*.class"/> 
      </fileset> 
     </batchtest> 
संबंधित मुद्दे