With Ant, you need to run JUnit-test. To use this code:
<target name="tempTest">
<mkdir dir="<folder in which to create the class-file>" />
<javac destdir="<folder in which to create the class-file>"
srcdir="<folder in which the java-file with junit-test>"
debug="on"
memorymaximumsize="128m"
fork="true"
encoding="utf-8">
<classpath>
<fileset dir="<folder, which is located in junit.jar>" includes="junit.jar"/>
</classpath>
</javac>
<junit printsummary="on" haltonfailure="no">
<classpath>
<fileset dir="<folder, which is located in junit.jar>" includes="junit.jar"/>
<pathelement location="<folder in which to create the class-file (in this step, class-file has been created in this folder)>"/>
</classpath>
<formatter type="plain"/>
<batchtest todir="<folder in which the file will be created for output to a test error>">
<fileset dir="<folder in which the java-file with junit-test>"/>
</batchtest>
</junit>
</target>
Code class take the test:
package ForTests;
import org.junit.Test;
public class TempTest {
#Test
public void testTemp() {
System.err.print("ERROR FOR TESTS");
}
}
However, when you run the ant-script file displays the error:
Testsuite: TempTest
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
Caused an ERROR
TempTest
java.lang.ClassNotFoundException: TempTest
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
Tell me, please, how to solve the problem?
UPD:
Issue has been resolved.
In order to run junit-test line must
<batchtest todir="<folder in which the file will be created for output to a test error>">
<fileset dir="<folder in which the java-file with junit-test>"/>
</batchtest>
replaced
<batchtest todir="<folder in which the file will be created for output to a test error>">
<fileset dir="<folder in which to create the class-file (in this step, class-file has been created in this folder)>"/>
</batchtest>
Related
I have seen a few links which appears to imply that JUnit tests can be executed from the .java file instead of the .class
For example
<junit printsummary="yes" haltonfailure="yes" haltonerror="yes">
<classpath refid="ui.tests.classpath"/>
<formatter type="xml"/>
<batchtest todir="${env.WORKSPACE}/UITests/output">
<fileset dir="${ui.tests.classes}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
Instead of
<junit printsummary="yes" haltonfailure="yes" haltonerror="yes">
<classpath refid="ui.tests.classpath"/>
<formatter type="xml"/>
<batchtest todir="${env.WORKSPACE}/UITests/output">
<fileset dir="${ui.tests.classes}">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
</junit>
Is the first example a valid case? I could not get it working due to ClassNotFoundExceptions
I agree with the comment above, that it looks like the tag allows for either .java or .class.
I did a small test and when I ran a test with this setup - where the dir=somefolder, then using <include name="**/*Test.java"/> when the folder pointed at contains class files, then Ant will basically have an empty fileset to process for *Test.java, but when using <include name="**/*Test.class"/> then the fileset was not empty and the test cases will get run.
This was the result of my quick test. As far as I could tell, it looks like you need to specify the *Test.class to pickup the test cases.
I've been scratching my head at this for a while now. I have been looking at all related posts on Stack Overflow and the ones I could find with Google but it was to no avail.
I'm trying to build a Java program that has Mancala.java as main class. The directory structure is as follows: a folder called mancala with one subfolder called test and one subfolder called mancala_test. The test folder has the Mancala.java file and other files and the mancala_test folder contains the JUnit test file called MancalaTest.java. In Eclipse the test file runs, but when running via Ant I get the following error:
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
I'm using the following build file in the mancala folder:
<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>
Other possible relevant information is that the Mancala.java file contains two static initializers being the GUI and the Mancala class itself (e.g., static Mancala mancala; static GUI gui; and the Mancala_test.java just uses a Mancala mancala = new Mancala() object in each test.
An example of one test:
#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);
}
It probably has something to do with the classpaths (I tried every possible variation I could think of) or the static stuff. I would be very grateful if someone could put me out of my misery.
/edit Directory structure after build: http://i.imgur.com/vvFJtNB.png
You need a target to compile the test_mancala directory and add the destination of that compile to your runjunit target's classpath.
<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>
I've been scratching my head over this for a while now (Googled a bunch, looked through other related SO posts to no avail). I have a Java program comprised of two files, Logic and Tests. Tests contains about a hundred JUnit tests, and I've gotten 100% success rate with said tests by calling javac *.java followed by java org.junit.runner.JUnitCore Tests. However when I run my build.xml with a simple ant -verbose test (in order to follow the output since I'm new to all this), I get the following output:
[junit] Testsuite: Tests
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Null Test: Caused an ERROR
[junit] Tests
[junit] java.lang.ClassNotFoundException: Tests
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
[junit]
[junit]
[junit] Test Tests FAILED
BUILD SUCCESSFUL
My build.xml is as follows:
<project name="ETL_Automation" default="test" basedir=".">
<path id="classpath.base">
</path>
<path id="classpath.test">
<pathelement location="${basedir}/mysql-connector-java-5.1.18-bin.jar" />
<pathelement location="${basedir}/junit-4.10.jar"/>
<path refid="classpath.base" />
</path>
<target name="compile">
<javac srcdir="${basedir}">
<classpath refid="classpath.test"/>
</javac>
</target>
<target name="test" depends="compile">
<junit fork="no">
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<batchtest>
<fileset dir="${basedir}/" includes="Tests.class" />
</batchtest>
</junit>
</target>
<target name="clean" depends="test">
<delete>
<fileset dir="${basedir}" includes="*.class"/>
</delete>
</target>
The directory structure is pretty straightforward. Tests.java, Logic.java, junit-4.10.jar, mysql-connector-java-5.1.18-bin.jar, build.xml, and a referenced .properties file are all in the same folder. The java code references external files but those are unrelated to this particular issue. I don't know if the classpath could be the cause of this issue (as I'm pretty convinced what I currently have doesn't work).
Thanks!
You will need to add the directory with the Tests.class to the classpath.tests classpath (which is ${basedir} in your setup)
Try:
<path id="classpath.test">
<pathelement location="${basedir}/mysql-connector-java-5.1.18-bin.jar" />
<pathelement location="${basedir}/junit-4.10.jar"/>
<pathelement location="${basedir}" />
<path refid="classpath.base" />
</path>
I put up an Ant project which includes a unit test using JUnit.
The test target is as:
<target name="test">
<mkdir dir="target/test/reports"/>
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${test.classes.dir}"/>
<pathelement location="${test.junit.jar}" />
<pathelement path="${classes.dir}"/>
<pathelement path="${java.class.path}"/>
</classpath>
<formatter type="plain"/>
<batchtest fork="yes" todir="${test.reports.dir}">
<fileset dir="${test.src.dir}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
When I run this test, it show on the screen only the summary of the test like:
Buildfile: F:\test\build.xml
test:
[junit] Running com.mycompany.myproject.myTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.013 sec
[junit] Running com.mycompany.myproject.myTest1
[junit] Tests run: 3, Failures: 1, Errors: 0, Time elapsed: 0.018 sec
BUILD FAILED
F:\test\build.xml:30: Test com.mycompany.myproject.myTest1 failed
Total time: 1 second
Is there anyway I can tell JUnit or Ant to display the detailed result on the screen?
Also, if I want to write something in the Unit test to the screen, how can I do this? I tried to insert System.out.println() in the test but it does not display anything on the screen.
Many thanks.
Set the showOutput flag to true.
What are you trying to accomplish via the S.o.p in the middle of a test?
change printsummary value to withOutAndErr, that will cause JUnit to print System.out and System.err text
IMHO, you are solving the wrong problem.
The junit results are collected and sitting "${test.reports.dir}" to be 'seen' by you. Ant has task that could help you in getting an HTML report
Introduce a target to generate html from the collected data (they are XML files)
<junitreport todir="./reports">
<fileset dir="${test.reports.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="./report/html"/>
</junitreport>
Also, if you want to print something to the screen, just use
<echo message="My message..." />
<batchtest fork="yes" todir="${junit.report.dir}">
<formatter type="xml" />
<fileset dir="${application.build.stage.java.class.dir}">
<include name="**/*Test.class" />
</fileset>
</batchtest>
You can use this in your build.xml to generate the reports as html files.
I have a directory with a bunch of JUnit tests in my project. So far I have used separate target for each unit test. For example:
<target name="MyTest">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml"/>
<test name="tests.MyTest" todir="${junit.output.dir}"/>
<classpath refid="MyProject.classpath"/>
</junit>
</target>
This method requires me to change build file every time I add a Unit test.
I want to able able to to run all unit tests in the project with a single Ant builder target. Is it possible to do?
Yep it is, you need to look at the fileset tag, e.g:
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${MyProject.classpath}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
The important part is the use of fileset and a glob/wildcard pattern to match the names of the tests. Full docs on the junit task with examples here:
http://ant.apache.org/manual/Tasks/junit.html
Yep! We do it using an ant command batchtest. Looks like this:
<batchtest todir="${junit.report.dir}">
<fileset dir="${basedir}\test\unit">
<include name="**/*Test.java" />
</fileset>
</batchtest>
Google it, it should sort you out