I'm writing my build.xml. However, it seems there is something go wrong with my junit task. when I run my junit task. I can build success, but the junit report only show that run 1 test with error. But I have more than 10 tests. So I wonder if there is something run with my junit task. Here is my code.
<property name="src.dir" value="src"/>
<property name="bin.dir" value="bin"/>
<property name="dest.dir" value="dest"/>
<property name="test.dir" value="test/>
<property name="lib.dir" value="lib"/>
<path id="classpath">
<pathelement location="${lib.dir}/junit-4.11.jar"/>
<pathelement location="${lib.dir}/ant-junit4.jar"/>
</path>
<target name="test" depends="compile">
<junit printsummary="yes" haltonfailure="no">
<classpath>
<pathelement location="${bin.dir}"/>
<path refid="classpath"/>
</classpath>
<formatter type="plain" usefile="false"/>
<batchtest fork="yes">
<fileset dir="${test.dir}" includes="*Test*.java"/>
</batchtest>
</junit>
</target>
I cannot figure out what is wrong so could somebody help me out?
And what is happening? Do you get any error messages?
You usually need to do the following:
Compile your normal code. The resulting *.class files should be placed inside a directory such as target/classes or build/classes. Use the destdir parameter of the <javac> task to do this.
Compile your JUnit tests.
In your classpath, you need all of the jars you needed to compile your normal classes
You need a reference to the destdir where your normal classes were compiled to.
You need the JUnit jar.
These should be compiled to a different directory from your normal jars. Normally, this is target/test-classes or build/test-classes.
Once you've compiled the JUnit tests, you may run them. You can use the <junit> task like you did.
You need to make sure that the includeantruntime parameter is set to true
You should also set fork to true.
You need the same classpath (with all three elements you had) when you compiled the test classes. More jars might be needed, but usually not.
You run the tests on the compiled JUnit test classfiles (the ones you saved to target/test-classes or build/test-classes. In your example, you're trying to run them against the source.
I use the Maven standards for my directory layout. That means my Java source is under src/main/java while my JUnit Java files are under src/test/java. Any XML or properties or other none source files needed are stored in src/main/resources. The regular source is compiled to target/classes while the Junit sources are compiled to target/test-classes.
This makes it easy to compile your code and test code separately without worrying about **/test/**, **/Test/**, **/JUnit/** exceptions in directory compiling since everything is separate.
Hope this helps.
Related
An Ant junit task that has worked for months is suddenly failing with a NoClassDefFoundError for classes that used to be found. Is there a way to display the classpath that is built in the junit task?
<target name="basic-junit-test" description="Run a single JUnit test. ">
<junit printsummary="yes" fork="no" haltonfailure="yes">
<classpath>
<pathelement location="target/WEB-INF/lib/log4j-1.2.16.jar"/>
.
. many other pathelements
.
</classpath>
<test name="com.mycompany.command.TestUNLOCKACCOUNTCommand" outfile="${report.dir}/junit_test_results" />
</junit>
</target>
I'm a big fan of declaring my Ant paths at the top of my build and using classpath references in the various tasks.
To pathconvert task can be used to print the classpath contents as a property:
<path id="test.path">
<pathelement location="target/WEB-INF/lib/log4j-1.2.16.jar"/>
.
. many other pathelements
.
</path>
<target name="echo-path" description="Echo test path">
<pathconvert targetos="unix" property="test.path.unix" refid="test.path">
<echo message="Test path: ${test.path.unix}"/>
</target>
<target name="basic-junit-test" depends="echo-path" description="Run a single JUnit test. ">
<junit printsummary="yes" fork="no" haltonfailure="yes">
<classpath>
<path refid="test.path"/>
</classpath>
<test name="com.mycompany.command.TestUNLOCKACCOUNTCommand" outfile="${report.dir}/junit_test_results" />
</junit>
</target>
Update
Just occurred to me: an even simpler solution might be to run Ant in debug mode.
This is not really the answer to my question, Mark O'Connor and Rebse gave excellent answers, instead this is a more thorough explanation of what happened to cause me to ask the question in the first place. I had an ANT Junit task that I had used to develop about 100 controller classes. I used it for the first time in several months and every test failed with a classNotFound exception. The class that was not found was one that I was sure should be on the classpath, it was a locally created jar file that is automatically picked up for the build. I thought that somehow the classpath was at fault so I wanted to display it when a test was run.
After many attempts to figure out what was going on I put a try block around the code that was producing the classNotFound exception and I saw that the local class was not the class that was not being found. This lead me to a search of the lib directory and eventually (after about six hours) I realized that the problem was that I had replaced an older version of slf4j-api with a newer version. There was a dependency on a method that was in the older version but not in the newer.
Mark's answer just helped me, but just a quick note as I needed to close the pathconvert xml element when copying the example in the answer.
<pathconvert targetos="unix" property="test.path.unix" refid="test.path" />
I was just starting to integrate Cobertura into our main product's build process which uses Ivy as the dependency management tool. There are several libraries lib-a, lib-b and lib-c that do not have test cases and one project that depends on these libs and contains the unit and integration tests for all of these libraries.
Generally, instrumenting, running the instrumented tests and generating the Cobertura report works. However, there are several problems:
During instrumentation, there are several warnings (about 10) reported:
Problems instrumenting archive entry: a.b.c.MyClassFoo java.lang.RuntimeException: java.lang.ClassNotFoundException: a.b.c.MyClassBar
However, the reported class is exisiting. At the end of instrumentation, it reports
Saved information on 364 classes
When viewing the report, it shows all classes, but all classes of dependent libs are reported as 0% coverage.
When viewing details, it reports that no sources where found.
Now I think the problem might be, that Cobertura has problems with
- instrumenting the class files provided as jar files and
- Getting the sources from jar files
The instrumentation ant task in my build.xml does the following:
<cobertura-instrument todir="${build.dir}/instrumented-classes">
<includeClasses regex="com\.mycompany.*" />
<instrumentationClasspath>
<path refid="default.test.classpath" />
<pathelement location="${build.classes.dir}" />
</instrumentationClasspath>
</cobertura-instrument>
Should this be sufficient?
Im wondering, because of the reported warnings. All reported classes can be found within the jars.
For the second problem, I don't even know how to provide sources as jars to cobertura-report...
I tried
<cobertura-report destdir="${build.dir}/coverage">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<ivy:cachefileset conf="runtime-test" type="sources"/>
</cobertura-report>
but it says cachefileset is not supported. I also tried using a pathid which I also can't provide in cobertura-report.
Do I have to unzip all sources before (which would be very time-consuming) and then provide them as a normal fileset?
Ok, while refactoring my Cobertura Ant tasks, I was able to solve the problem with dependency classes reported as 0% coverage.
At first my test task contained the following:
<junit printsummary="yes" haltonfailure="no">
<classpath>
<pathelement path="${build.test.classes.dir}"/>
<pathelement path="${build.dir}/instrumented-classes"/>
<pathelement path="${build.classes.dir}"/>
<path refid="default.test.classpath"/>
<path refid="cobertura.classpath"/>
</classpath>
(...)
</junit>
The problem was, that instrumenting the jar files provided by ivy results in jar files within the ${build.dir}/instrumented-classes directory. However, pathelement supposedly only looks for class files. So I added a fileset to include the instrumented jars as well:
<junit printsummary="yes" haltonfailure="no">
<classpath>
<pathelement path="${build.test.classes.dir}"/>
<pathelement path="${build.dir}/instrumented-classes"/>
<fileset dir="${build.dir}/instrumented-classes">
<include name="*.jar" />
</fileset>
<pathelement path="${build.classes.dir}"/>
<path refid="default.test.classpath"/>
<path refid="cobertura.classpath"/>
</classpath>
(...)
</junit>
The other problems still remain.
I'm developing a Java project using Eclipse, and Ant as a build tool. When I run "ant all" from the command line, my project builds without any errors, but on Eclipse I get many compilation errors.
So I thought I'd copy Ant's Classpath onto my Eclipse Project's Build Path.
Is there an Ant task/command to show that? Like "ant just show me your assembled classpath" or something?
If you run Ant with the -verbose and -debug flags, you'll see all gory details of what javac is doing, including the classpath.
I would introduce a task for printing the classpath, and call that task with antcall. The classpath would be given as a parameter to that task.
You can do something like this in your target, so for example
lets say you've defined your classpath as
<path id="project.classpath">
<fileset dir="${SERVER_DEV}/classes">
<include name="*.zip"/>
<include name="*.jar"/>
</fileset>
<pathelement location="${SERVER_DEV}/3rdParty/jre/NT/1.5.0/lib/jsse.jar"/>
</path>
then you can do something like
<target name="compile" depends="init" description="Compiles All Java Sources">
<property name="myclasspath" refid="project.classpath"/>
<echo message="Classpath = ${myclasspath}"/>
<javac ...>
....
</javac>
</target>
It will print out the classpath used to run the specific target
I am using sonar to measure code quality. One thing that I do not know is the steps to measure code coverage using Cobertura.
I followed the steps from http://cobertura.sourceforge.net/anttaskreference.html and was able to generate xml files. How do I get these xml files into SONAR?
Is there an easier way to use Cobertura in SONAR?
I am running the code coverage (Cobertura) in a different server than my SONAR server. Both servers are running under LINUX.
Thanks for the help!
You configure the Sonar task to upload unit test and cobertura reports generated by other parts of your build logic.
This is in contrast to Maven which has a standard build life-cycle that Sonar is able to leverage.
Unit test and code coverage
The following logic runs the unit tests with cobertura instrumented classes. An XML coverage report is generated by cobertura at the end:
<target name="instrument-classes" depends="compile-tests">
<taskdef resource="tasks.properties" classpathref="test.path"/>
<cobertura-instrument todir="${instrumented.classes.dir}" datafile="${build.dir}/cobertura.ser">
<fileset dir="${classes.dir}"/>
</cobertura-instrument>
</target>
<target name="junit" depends="instrument-classes">
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="test.path"/>
<pathelement path="${instrumented.classes.dir}"/>
<pathelement path="${test.classes.dir}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${test.reports.dir}">
<fileset dir="${test.src.dir}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="test" depends="junit">
<cobertura-report format="xml" datafile="${build.dir}/cobertura.ser" destdir="${cobertura.reports.dir}"/>
</target>
Invoking Sonar
I normally use a very simple Sonar target:
<target name="sonar" depends="test">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpathref="sonar.path"/>
<sonar:sonar key="${sonar.project.key}" version="${sonar.project.version}" xmlns:sonar="antlib:org.sonar.ant"/>
</target>
And use a properties file to control all aspects of Sonar's behaviour:
sonar.project.key=org.demo:demo
sonar.project.version=1.0-SNAPSHOT
sonar.projectName=Demo project
sonar.host.url=http://myserver:9000
sonar.jdbc.url=jdbc:mysql://myserver:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.sources=${src.dir}
sonar.tests=${test.src.dir}
sonar.binaries=${classes.dir}
sonar.dynamicAnalysis=reuseReports
sonar.surefire.reportsPath=${test.reports.dir}
sonar.java.coveragePlugin=cobertura
sonar.cobertura.reportsPath=${cobertura.reports.dir}/coverage.xml
Demonstrates how Sonar can be configured to pick up the unit test reports created by junit and the code coverage report generated by cobertura.
The build does not have to run on the same server as Sonar. In that case one must provide the remote Sonar URL and JDBC credentials.
You would have to add these properties to Sonar's pom.xml:
<properties>
<sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
<sonar.phase>generate-sources</sonar.phase>
<sonar.surefire.reportsPath>target/reports/test/</sonar.surefire.reportsPath>
<sonar.cobertura.reportPath>../project/target/reports/coverage/coverage.xml</sonar.cobertura.reportPath>
</properties>
(with paths appropriate to your environment)
And run:
mvn sonar:sonar
Check the user list for more details.
if you're using Maven, then you do not have anything special to specify in your POM file. Just run "mvn clean sonar:sonar" and Sonar will automatically compile your code, run your tests with Cobertura (which is the default coverage engine in Sonar) and push all the results in the DB.
Same if you're using Ant [1] or the simple java runner [2] instead of Maven.
I do insist on the fact that you do not have to manually run Cobertura (with an Ant task for instance) previously to running Sonar.
[1] http://docs.codehaus.org/display/SONAR/Analyzing+with+Sonar+Ant+Task
[2] http://docs.codehaus.org/display/SONAR/Analyse+with+a+simple+Java+Runner
Fabrice,
SonarSource
In my project, I have a src folder with code source of the application and test folder with code source of the application tests.
In my Ant build, I would like to separate javadoc generation of these source codes. For the src code javadoc generation, there is no problem but for the tests code javadoc generation, I've got a problem because test code uses src code.
My Ant task to generate javadoc is like that :
<path id="classpath-test">
<pathelement path="." />
<pathelement path="${testclasses.home}" />
<pathelement path="${classes.home}" />
<fileset dir="${lib.home}" includes="*.jar" />
<fileset dir="${libtest.home}" includes="*.jar" />
</path>
<target name="compile" ... > // compiles src code of the project in ${classes.home}
<target name="compile-tests" depends="compile">
<javac srcdir="${test.home}"
destdir="${testclasses.home}"
target="1.5"
source="1.5"
debug="true"
>
<classpath refid="classpath-test" />
</javac>
<copy todir="${testclasses.home}">
<fileset dir="${test.home}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="generate-javadoc-tests" depends="compile-tests" >
<javadoc sourcepath="${test.home}" packagenames="*"
destdir="${test-javadoc.home}" verbose="false"
linksource="true" encoding="${encoding}">
<classpath refid="classpath-test" />
</javadoc>
</target>
The ${test.home} variable is test folder. In the classpath-test, I put jar from junit to avoid error about annotation specifics to junit during javadoc generation. This jar is contained in ${libtest.home}.
When I generate javadoc, I have several warnings about code from test folder that using code from src folder which is normal. The errors are like that :
[javadoc] E:\workspace\app\test\com\app\MyClass.java:9: package com.app.SrcClass does not exist
[javadoc] symbol : class MyClass
[javadoc] location: class com.app.MyClass
So, someone knows a way to includes src classes in classpath to avoid these warnings but without having source code javadoc included in test code javadoc.
Or may be a way to disable these warnings because the verbose option of javadoc task to false doesn't disable these warnings.
So, someone knows a way to includes src classes in classpath to avoid
these warnings but without having source code javadoc included in test
code javadoc.
Make sure the classes on which your tests depend are on the classpath. You might want to make your javadoc generation target dependent on the target which compiles code from src and builds a jar file. Then make sure that the classpath, referenced by refid classpath-test includes that jar.