[junit] java.lang.NoClassDefFoundError:
已回答
There are no useful support articles/tickets in here, so I am posting my case with hopping to get a solution.
The project was created in IntelliJ 2017 and should be all the references are added properly ( JDK, Junit ). Building in Intellj passes without errors.
The project I am running from command line, compilation passes but running the tests are failing with java.lang.NoClassDefFoundError
Here is the build.xml file I used for Ant automation framework.
<project name="Intellij_ANT_project_example">
<property name="main.src.dir" value="src/main/java"/>
<property name="test.src.dir" value="src/test/java"/>
<property name="main.build.dir" value="build/"/>
<property name="test.build.dir" value="build/"/>
<path id="classpath.test">
<pathelement location="lib/junit-4.12.jar"/>
<pathelement location="lib/hamcrest-core-1.3.jar"/>
<pathelement location="${main.build.dir}"/>
</path>
<target name="compile">
<mkdir dir="${main.build.dir}"/>
<javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false"/>
</target>
<target name="test-compile" depends="compile">
<mkdir dir="${test.build.dir}"/>
<javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false">
<classpath refid="classpath.test"/>
</javac>
</target>
<target name="test" depends="test-compile">
<junit printsummary="on" haltonfailure="yes" fork="true">
<classpath>
<path refid="classpath.test"/>
<pathelement location="${test.build.dir}/test/java"/>
</classpath>
<formatter type="brief" usefile="false" />
<batchtest>
<fileset dir="${test.src.dir}" includes="**/*Test.java" />
</batchtest>
</junit>
</target>
</project>
And here is the Java source codes I used for this automation:
package main.java;
public class Calculator
{
public int evaluate(String expression)
{
int sum = 0;
for (String summand: expression.split("\\+"))
{
sum += Integer.valueOf(summand);
}
return sum;
}
}
package test.java;
import static org.junit.Assert.assertEquals;
import main.java.Calculator;
import org.junit.Test;
public class CalculatorTest
{
@Test
public void evaluatesExpression()
{
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
assertEquals(6, sum);
}
}
while I run 'ant test' from command line I get the following output:
$ ant test
Buildfile: /home/khachatryan/Intellij_ANT_project_example/build.xml
compile:
[javac] Compiling 1 source file to /home/khachatryan/Intellij_ANT_project_example/build
test-compile:
[javac] Compiling 1 source file to /home/khachatryan/Intellij_ANT_project_example/build
test:
[junit] Running CalculatorTest
[junit] Testsuite: CalculatorTest
[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] Null Test: Caused an ERROR
[junit] CalculatorTest (wrong name: test/java/CalculatorTest)
[junit] java.lang.NoClassDefFoundError: CalculatorTest (wrong name: test/java/CalculatorTest)
[junit] at java.lang.ClassLoader.defineClass1(Native Method)
[junit] at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
[junit] at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
[junit] at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
[junit] at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:332)
[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:265)
[junit]
[junit]
BUILD FAILED
/home/khachatryan/Intellij_ANT_project_example/build.xml:26: Test CalculatorTest failed
请先登录再写评论。
Hi, from which folder are you running the command 'ant test'? On first glance, it looks like a relative path problem.
I tried to point absolute paths as well by running simple 'java' command from console and got error.
Compilation creates Java classes by the following paths:
/home/khachatryan/Intellij_ANT_project_example/out/production/Intellij_ANT_project_example/test/java/ ===> CalculatorTest.class
and
/home/khachatryan/Intellij_ANT_project_example/out/production/Intellij_ANT_project_example/main/java/ ===> Calculator.class
I didn't realize why while I try to run CalculatorTest by running "java -cp "/home/khachatryan/Intellij_ANT_project_example/out/production/Intellij_ANT_project_example/test/java/*:/home/khachatryan/Intellij_ANT_project_example/out/production/Intellij_ANT_project_example/main/java/*" CalculatorTest returns error "Could not find or load main class CalculatorTest"
From IntelliJ IDE the automation project runs as expected:
Here is what I have in CalculatorTest.java file:
The problem is, why can't I run from command line and get the same output?
Adding paths for junit-4.12.jar and hamcrest-core-1.3.jar in classpath fixed the issue with running from command line a java command.
But still I can't run the 'ant test' command.
Changed the 'classpath' in build.xml file to the following:
You may have better results with getting help regarding Ant build.xml file configuration at http://stackoverflow.com/. Share the sample project (http://stackoverflow.com/help/mcve) so that others can check it and help you faster.