Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ant在运行在Eclipse中工作的JUnit测试时抛出ClassNotFoundException_Java_Eclipse_Ant_Build_Junit - Fatal编程技术网

Java Ant在运行在Eclipse中工作的JUnit测试时抛出ClassNotFoundException

Java Ant在运行在Eclipse中工作的JUnit测试时抛出ClassNotFoundException,java,eclipse,ant,build,junit,Java,Eclipse,Ant,Build,Junit,这件事让我挠头已有一段时间了。我一直在看所有关于Stack Overflow的相关文章,以及那些我可以通过Google找到的文章,但都没有用 我正在尝试构建一个以Mancala.Java为主类的Java程序。目录结构如下:一个名为mancala的文件夹,其中一个子文件夹名为test,另一个子文件夹名为mancala_test。测试文件夹包含Mancala.java文件和其他文件,Mancala_测试文件夹包含名为MancalaTest.java的JUnit测试文件。在Eclipse中,测试文件会

这件事让我挠头已有一段时间了。我一直在看所有关于Stack Overflow的相关文章,以及那些我可以通过Google找到的文章,但都没有用

我正在尝试构建一个以Mancala.Java为主类的Java程序。目录结构如下:一个名为mancala的文件夹,其中一个子文件夹名为test,另一个子文件夹名为mancala_test。测试文件夹包含Mancala.java文件和其他文件,Mancala_测试文件夹包含名为MancalaTest.java的JUnit测试文件。在Eclipse中,测试文件会运行,但在通过Ant运行时,会出现以下错误:

init:

compile:
    [javac] Compiling 6 source files to C:\Users\[me]\Desktop\build

runjunit:
    [junit] Running mancala_test.MancalaTest
    [junit] Testsuite: mancala_test.MancalaTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
    [junit]
    [junit]     Caused an ERROR
    [junit] mancala_test.MancalaTest
    [junit] java.lang.ClassNotFoundException: mancala_test.MancalaTest
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:266)
    [junit]
    [junit] Test mancala_test.MancalaTest FAILED

BUILD SUCCESSFUL
Total time: 1 second
我正在mancala文件夹中使用以下生成文件:

<project default="runjunit" name="Compile and run JUnit tests">

  <target name="clean">
     <delete dir="build"/>
  </target>
  <target name="clean2">
     <delete dir="build"/>
  </target>

  <target name="init">
    <record name="build.log" loglevel="verbose" append="false"/>
  </target>

  <target name="runjunit" depends="compile">
    <junit printsummary="on">
      <test name="mancala_test.MancalaTest"/>
      <classpath>
        <pathelement location="build"/>
      </classpath>
   <formatter
      type="plain"
      usefile="false"
    />
    </junit>
  </target> 

  <target name="compile" depends="init">
    <mkdir dir="build"/>
      <javac includeantruntime="false" srcdir="./test" destdir="build"/>
  </target>

</project>

其他可能的相关信息是Mancala.java文件包含两个静态初始值设定项,即GUI和Mancala类本身(例如,静态Mancala Mancala;静态GUI GUI;Mancala_test.java在每个测试中只使用一个Mancala Mancala=new Mancala()对象)

一个测试的示例:

@Test
public void testAmountOfSeed() {
    Mancala mancala = new Mancala();        
    mancala.divideBoard();

    int totalAmountOfSeed = 0;
    for (int i = 0; i < mancala.gameBoard.size(); i++) {
        totalAmountOfSeed +=mancala.gameBoard.get(i).getSeed();
    }
    assertTrue("Total amount of seed in initial condition not 48!", totalAmountOfSeed == 48);
}
@测试
种子的公共有效期(){
Mancala Mancala=新Mancala();
mancala.隔板();
int totalAmountOfSeed=0;
对于(int i=0;i
它可能与类路径(我尝试了我能想到的所有可能的变化)或静态的东西有关。如果有人能让我摆脱痛苦,我将非常感激


/编译后编辑目录结构:

您需要一个目标来编译
test\u mancala
目录,并将该编译的目标添加到
runjunit
目标的类路径中

<target name="compile-test_mancala" depends="init, compile">
  <mkdir dir="build-test_mancala"/>
  <javac includeantruntime="false" srcdir="./test_mancala" destdir="build_mancala">
    <classpath>
      <pathelement location="build"/>
      <pathelement location="${junit_lib}"/>
    </classpath>
  </javac>
</target>

<target name="runjunit" depends="compile, compile-test_mancala">
  <junit printsummary="on">
    <test name="mancala_test.MancalaTest"/>
    <classpath>
      <pathelement location="build"/>
      <pathelement location="build-test_mancala"/>
    </classpath>
 <formatter
    type="plain"
    usefile="false"
  />
  </junit>
</target> 


你能显示你的源目录和目标目录结构吗?看。哎哟…我忘了把
编译的输出放在新的
编译测试(compile-test)mancala
的类路径中。现在编辑…这也提醒我你需要类路径中的junit.jar文件。我使用了
${junit lib}
作为该值的占位符。或者用路径替换它,或者用路径定义
junit_lib
属性。再次编辑…将
compile
添加到
依赖对象的列表中。看起来您还需要hamcrest.org中的jar。在eclipse安装目录中查找它。将其添加到类路径是junit任务的一部分。没问题!很高兴能帮助任何不幸与Ant合作的人。