gpt4 book ai didi

java - 用于运行显示 java.lang.ClassNotFoundException 的 junit 测试的 Ant 脚本

转载 作者:行者123 更新时间:2023-12-02 03:02:19 25 4
gpt4 key购买 nike

我的 Java JUnit 测试位于以下结构上:src/com/project/test/test_junit.java

我的 Ant 脚本是:

<project name="Test" basedir="." default="unit-test">


<property name="src" value="src"/>
<property name="build-test-classes" value="${src}/com/project/test"/>
<property name="lib" value="lib"/>
<property name="junit4" value="lib/junit-4.12.jar"/>

<target name="unit-test">

<junit printsummary="yes" haltonfailure="yes">
<formatter type="plain" usefile="false"/>
<classpath>
<pathelement location="${junit4}"/>
<pathelement location="${build-test-classes}"/>
</classpath>
<batchtest>
<fileset dir="${build-test-classes}">
<include name="**/*.java"/>
</fileset>
</batchtest>
</junit>
</target>
</project>

PS:我更改了一些名称以编写不太具体的代码。

运行 Ant 脚本时,我遇到以下问题:

unit-test:
[junit] Running test_junit
[junit] Testsuite: test_junit
[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] test_junit
[junit] java.lang.ClassNotFoundException: test_junit
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:348)
[junit]

BUILD FAILED
/build_junit_test.xml:23: Test AccountGS_Junit failed
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.actOnTestResult(JUnitTask.java:1959)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:858)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1904)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:804)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

Total time: 0 seconds

我已经在这里关注了很多问题,但似乎任何答案都解决了我的问题。

最佳答案

我敢打赌你还没有编译测试代码。提示是 build-test-classes 属性,它指向源代码树中的文件:

<property name="src" value="src"/>
<property name="build-test-classes" value="${src}/com/project/test"/>

示例

创建编译源代码的目标

<target name="compile" depends="resolve,resources" description="Compile code">
<mkdir dir="${build.dir}/classes"/>
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" includeantruntime="false" debug="true" classpathref="compile.path"/>
</target>

创建编译测试代码的目标

<target name="compile-tests" depends="compile" description="Compile tests">
<mkdir dir="${build.dir}/test-classes"/>
<javac srcdir="${test.src.dir}" destdir="${build.dir}/test-classes" includeantruntime="false" debug="true">
<classpath>
<path refid="test.path"/>
<pathelement path="${build.dir}/classes"/>
</classpath>
</javac>
</target>

然后 Junit 应该可以工作

<target name="test" depends="compile-tests" description="Run unit tests">
<mkdir dir="${build.dir}/test-reports"/>
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="test.path"/>
<pathelement path="${build.dir}/classes"/>
<pathelement path="${build.dir}/test-classes"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${build.dir}/test-reports">
<fileset dir="${test.src.dir}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
</target>

关于java - 用于运行显示 java.lang.ClassNotFoundException 的 junit 测试的 Ant 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42276795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com