Executing Junit Tests in ant - noclassdeffounderror wrong name - junit

I'm trying to execute a few junit test suits with ant.
Here's my folder structure
bin
lib
src
test
build.xml
First of all I compile all files in source location, later on java files in test folder.
Whole structure of test folder (compiled .class files) are saved in bin folder which looks like this inside.
bin-test-alltests
|
-suites - SetupSuite.class
- StartSuite.class
in allTests folder are tests that are used in suites in suites folder
I'm trying to start those suites but constantly I've got an error:
ant junit java.lang.noclassdeffounderror wrong name
I'm pretty sure that's something wrong with the class path but I don't know what. I've tried to changed
<test name="SetupSuite"/>
to
<test name="SetupSuite.class"/>
and I get another error:
java.lang.ClassNotFoundException: SetupSuite.class
Here's my build.xml file
<project name="MyTest" basedir=".">
<!--Common properties -->
<property name="src.dirname" value="src" />
<property name="test.dirname" value="test" />
<property name="lib.dirname" value="lib" />
<property name="bin.dirname" value="bin" />
<property name="src.encoding" value="utf8" />
<property name="src.version" value="1.7" />
<property environment="env" />
<!-- Paths for MyTest -->
<property name="MyTest.dir" value="${basedir}" />
<property name="MyTest.src.dir" value="${MyTest.dir}\${src.dirname}" />
<property name="MyTest.test.dir" value="${MyTest.dir}\${test.dirname}" />
<property name="MyTest.dest.dir" value="${MyTest.dir}\${bin.dirname}" />
<property name="MyTest.lib.dir" value="${MyTest.dir}\${lib.dirname}" />
<path id="classpath">
<pathelement location="${MyTest.lib.dir}\junit.jar" />
<pathelement location="${MyTest.test.dir}\test\suites\" />
</path>
<target name="compile-MyTest-src" >
<myjavac srcdir="${MyTest.src.dir}" destdir="${MyTest.dest.dir}" classpath="{MyTest.lib.dir}\junit.jar"/>
</target>
<target name="compile-MyTest-test" depends="compile-MyTest-src">
<path id='libs'>
<fileset dir= "${MyTest.lib.dir}\" includes="**/*.jar"/>
</path>
<myjavac srcdir="${MyTest.test.dir}\" destdir="${MyTest.dest.dir}" classpathref = 'libs'/>
</target>
<target name="execute-tests" depends="compile-MyTest-src,compile-MyTest-test">
<junit printsummary="yes" haltonfailure="no" showoutput="yes">
<formatter type = "brief" usefile = "false" />
<classpath refid="classpath" />
<test name="SetupSuite"/>
</junit>
</target>
<!-- General compiler settings -->
<presetdef name="myjavac">
<javac classpathref="classpath" debug="on" includeantruntime="false" encoding="${src.encoding}" source="${src.version}" target="${src.version}" />
</presetdef>
</project>

Solved. I had to add hamcrest-core-1.3.jar file to classpath

You need to set test name to full name of your SetupSuite:
<test name="alltests.suites.SetupSuite"/>

Related

JPA/Hibernate: Missing table error

I am trying to build a simple application using Hibernate JPA with MySQL but I keep getting a "Missing table" error whenever I try to deploy it to Wildfly.
My persistent class looks like this:
package com.example;
#Entity #Table
public class FooBar {
//...
}
Here is my persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ExampleJPA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.FooBar</class>
<properties>
<!-- Configuring JDBC properties -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydatabase" />
<property name="javax.persistence.jdbc.user" value="(my user)" />
<property name="javax.persistence.jdbc.password" value="(my password)" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<!-- Hibernate properties -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<!-- Configuring Connection Pool -->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="500" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="2000" />
</properties>
</persistence-unit>
</persistence>
And this is the message I get when deploying to a Wildfly 8 server:
org.jboss.msc.service.StartException in service jboss.persistenceunit.\"ExampleJPA.war#ExampleJPA\": org.hibernate.HibernateException: Missing table: FooBar
Caused by: org.hibernate.HibernateException: Missing table: FooBar
I do have a table called FooBar in the database. I can access it directly with JDBC using the same url, user and password defined in the persistence.xml. What is wrong with my JPA setup?
Your table doesn't exist in the schema, so the hibernate.hbm2ddl.auto value of "validate" fails. Either create the table or change the hibernate.hbm2ddl.auto value to "create" or "update".

for element in ant build.xml

I am using a build.xml(ant) and code looks as follows,
<junit fork="yes" dir="." >
----------
---------
<for list="1,2,3,4,5,6" param="Val">
<env key="environment" value="${Val}" />
<batchtest fork="yes" todir="${junitreport.todir}">
<fileset dir="src/java">
<include name="TestOne.java" />
<include name="TestTwo.java" />
</fileset>
</batchtest>
</for>
</junit>
while running this i get the following error,
junit doesn't support the nested "for" element.
Is there any other way to achieve this loop in junit?
Please help.
Swap the <for> and the <junit> elements so <for> is on the outside and <junit> is on the inside:
<for list="1,2,3,4,5,6" param="Val" delimiter=",">
<sequential>
<junit ...>
<!-- Use an at-sign to reference the "param" from "for". -->
<env key="environment" value="#{Val}" />
</junit>
</sequential>
</for>
Note, that Val is referenced as #{Val} with an at-sign (#), not a dollar sign ($).

In Cobertura Report all the percentages are 0 and message is coming have you mentioned the correct path

Here is my build.xml. Here all the paths are mentioned.
In report all the percentages are 0(ZERO).
And It is also prompting me that "Have you mentioned the specified source directory"
Please suggest where is the mistake.
Either I have to change the build.xml or have to perform some steps.
First I ran all the junit test cases, then stoped the server and then run the build.xml as ant-build.
<project name="RFM2" default="cobertura_REPORT">
<property name="BASEDIR" value="D:/SIT/busnessservice" />
<property name="cobertura.dir" value="D:/SIT/JUNIT_jars/cobertura-1.9.1" />
<property name="src.dir" value="${BASEDIR}/src" />
<property name="build.dir" value="${BASEDIR}/build" />
<property name="dist.dir" value="${BASEDIR}/dist" />
<property name="lib.dir" value="D:/SIT/JUNIT_jars/jars" />
<property name="report.dir" value="${BASEDIR}/reports" />
<property name="cobertura.ser.file" value="${cobertura.dir}/cobertura.ser" />
<property name="server.ser.file" value="D:/Program Files/cobertura.ser" />
<property name="instrumentedDir" value="${cobertura.dir}/instrument" />
<property name="junit.data.dir" value="${report.dir}/junit/data" />
<property name="junit.report.dir" value="${report.dir}/junit/html" />
<property name="junit.dir" value="D:/SIT/JUNIT_jars/jars" />
<property name="test.dir" value="D:/SIT/JUNITTESTFILES" />
<!--class path for cobertula -->
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura-1.9.4.1.jar" />
<include name="*.jar" />
</fileset>
</path>
<!--class path for JUNIT -->
<path id="junit.classpath">
<fileset dir="${junit.dir}">
<include name="**/*.jar" />
</fileset>
<pathelement location="${junit.dir}/junit-4.1.jar" />
<pathelement location="${BASEDIR}/bin" />
<path refid="cobertura.classpath" />
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<!-- PATH FOR LIB -->
<path id="lib.classpath">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<path id="runtime.classpath">
<pathelement location="${build.dir}" />
<path refid="lib.classpath" />
</path>
<!--Initialization -->
<target name="init">
<mkdir dir="${dist.dir}" />
</target>
<!-- Clean files-->
<target name="clean" description="Remove all generated files.">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Compile the java file -->
<!--Instrument the files -->
<target name="cobertura_instrument">
<cobertura-instrument todir="${instrumentedDir}" datafile="${server.ser.file}">
<fileset dir="${lib.dir}">
<include name="businessServices.jar" />
</fileset>
</cobertura-instrument>
<!-- Copy cobertula.ser file in server-->
</target>
<!--copy the instrumented file in class file-->
I have removed this block as I think it is not getting used .
But still my report is getting generated with zero percentage.
<!--Make EAR-->
<target name="making ear">
<echo>come in making ear</echo>
<ear destfile="${build.dir}/myapp.ear" appxml="${src.dir}/metadata/application.xml">
<fileset dir="${build.dir}" includes="*.jar,*.war" />
</ear>
</target>
<!--Run the JUNIT Test Case-->
<target name="runJunitTest">
<junit fork="yes" dir="${test.dir}" failureProperty="test.failed">
<!--
Specify the name of the coverage data file to use.
The value specified below is the default.
-->
<sysproperty key="net.sourceforge.cobertura.datafile" file="${server.ser.file}" />
<classpath location="${instrumentedDir}" />
<classpath refid="junit.classpath" />
<classpath refid="cobertura.dir" />
<formatter type="plain" usefile="false" />
<formatter type="xml"/>
<test name="${test.dir}/ColorConfigurationTest" />
<batchtest todir="${report.dir}" unless="testcase">
<fileset dir="${test.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</target>
<!--rEPORTING-->
<target name="cobertura_REPORT">
<delete dir="${report.dir}" />
<mkdir dir="${report.dir}" />
<cobertura-report srcdir="${src.dir}" format="html" destdir="${report.dir}" datafile="${server.ser.file}">
</cobertura-report>
</target>
</project>
<!--Make EAR-->
<target name="making ear">
<echo>come in making ear</echo>
<ear destfile="${build.dir}/myapp.ear" appxml="${src.dir}/metadata/application.xml">
<fileset dir="${build.dir}" includes="*.jar,*.war" />
</ear>
</target>
<!--Run the JUNIT Test Case-->
<target name="runJunitTest">
<junit fork="yes" dir="${test.dir}" failureProperty="test.failed">
<!--
Specify the name of the coverage data file to use.
The value specified below is the default.
-->
<sysproperty key="net.sourceforge.cobertura.datafile" file="${server.ser.file}" />
<classpath location="${instrumentedDir}" />
<classpath refid="junit.classpath" />
<classpath refid="cobertura.dir" />
<formatter type="plain" usefile="false" />
<formatter type="xml"/>
<test name="${test.dir}/ColorConfigurationTest" />
<batchtest todir="${report.dir}" unless="testcase">
<fileset dir="${test.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</target>
I noticed one thing that should be easy to correct - that doesn't match the Cobertura doc or my working copy. Hopefully this will fix your problem.
Your JUnit task references <classpath refid="cobertura.dir" /> but the cobertura.dir is not a path id, it is the name of a property pointing to your cobertura directory. (IntelliJ IDEA flagged it in red, which caught my eye!)
In my script, I have:
Then my JUnit task references the cobertura.classpath like so:
<classpath refid="cobertura.classpath" />
Hope this helps!

How to build a SWC with documentation

UPDATE
solved: embed doc with swc
solved: weird param names: param0, param1, etc.
I created a swc lib using compc.
Then I created the lib doc with asdoc.
But I dont know how to bind them together, since when I use the .swc in another project params names are weird (like myMethod(param0:Number)) and there is no doc description.
I'm using Ant, this is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<project name="uil" default="compile" basedir=".">
<property name="flexsdk" location="C:/sdks/flex_sdk_4.6/bin"/>
<property name="compc" location="${flexsdk}/compc.exe"/>
<property name="asdoc" location="${flexsdk}/asdoc.exe"/>
<property name="src" location="../src"/>
<property name="bin" location="../bin"/>
<target name="compile" depends="doc">
<exec executable="${compc}" failonerror="true">
<arg line="-debug=false" />
<arg line="-optimize=true" />
<arg line="-strict=true" />
<arg line="-locale=en_US" />
<arg line="-include-sources=${src}" />
<arg line="-output=${bin}/uil.swc" />
</exec>
</target>
<target name="doc">
<exec executable="${asdoc}" failonerror="true">
<arg line="-main-title 'UIL API Documentation'" />
<arg line="-window-title 'UIL API Documentation'" />
<arg line="-source-path ${src} -doc-sources ${src}" />
<arg line="-output ${bin}/uil-asdoc" />
</exec>
</target>
</project>
Edit: How it was solved
The line that make all the magic is this:
<taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar" />
Then I replaced all my <exec> tags to <compc> and <asdoc> and everything worked. You can see the entire code here.
Try to use zip ant target as in the build.xml of the Starling framework:
<!-- call asdoc to generate dita xml files -->
<asdoc output="${temp.dir}" lenient="true" failonerror="true" keep-xml="true" skip-xsl="true" fork="true">
<compiler.source-path path-element="${basedir}/src" />
<doc-sources path-element="${basedir}/src" />
</asdoc>
<!-- update swc with asdoc xml -->
<zip destfile="${deploy.dir}/${ant.project.name}.swc" update="true">
<zipfileset dir="${temp.dir}/tempdita" prefix="docs">
<include name="*.*"/>
<exclude name="ASDoc_Config.xml" />
<exclude name="overviews.xml" />
</zipfileset>
</zip>

How do I determine what target is calling my current target in Nant?

I am modifying a Nant build script to run some unit tests. I have different targets for locally run tests and tests to be run on team city.
<target name="run-unit-tests">
<property name="test.executable" value="tools\nunit\nunit-console.exe"/>
<call target="do-unit-tests"/>
</target>
<target name="run-unit-tests-teamcity">
<property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>
<call target="do-unit-tests"/>
</target>
in the target do-unit-tests I set up which test assemblies are run by setting a property and calling for NCover to do a code coverage run as follows:
<target name="do-unit-test">
<property name="test.assemblies" value="MyProject.dll">
<call target="do-unit-test-coverage" />
</target>
<target name="do-unit-test-coverage">
<ncover <!--snip -->
commandLineArgs="${test.args}"
<!--snip-->
</ncover>
</target>
As you can see in the ncover part I need a property called "test.args". This property depends on "test.assemblies"
ie: <property name="test.args" value="${test.assemblies} <!--snip -->" />
test.args needs to be set up differently between the locally run unit test and the one on team city...so I'm trying to figure out how to set this up.
if i put the property for test.args in "do-unit-test" after the property "test.assemblies" I can't specify one test.args if do-unit-test is called by run-unit-tests and another for run-unit-tests-teamcity.
I've been trying to do something like the following in "do-unit-test":
<if test="${target::exists('run-unit-tests-teamcity')}">
<property name="test.args" value="..." />
</if>
but obviously that doesn't work because the target will always exist.
What I'd like then is to test if my current target do-unit-test has been called by run-unit-tests-teamcity
Is this possible? I can't see it in the Nant documentation? Since its not there it either means that it will be a feature in the future or that I'm not understanding how things are specified in a Nant build script.
You can define properties in one target, and use their values in the other... For example, you can define
<target name="run-unit-tests">
<property name="test.executable" value="tools\nunit\nunit-console.exe"/>
<property name="test.extratestargs" value="foo,bar,baz"/>
<call target="do-unit-tests"/>
</target>
<target name="run-unit-tests-teamcity">
<property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>
<property name="test.extrtestargs" value="foo,baz,quux,xyzzy"/>
<call target="do-unit-tests"/>
</target>
<target name="do-unit-test-coverage">
<property name="test.args" value="${test.assemblies} ${test.extratestargs} <!--snip -->" />
<ncover <!--snip -->
commandLineArgs="${test.args}" >
<!--snip-->
</ncover>
</target>
Or if you need them to be structured completely differently, not just have some different values, take advantage of the fact that the property substitution is delayed:
<?xml version="1.0"?>
<project name="nanttest">
<target name="run-unit-tests">
<property name="test.executable" value="tools\nunit\nunit-console.exe"/>
<property name="test.args" value="foo bar -assembly ${test.assemblies} baz" dynamic="true"/>
<call target="do-unit-test"/>
</target>
<target name="run-unit-tests-teamcity">
<property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>
<property name="test.args" value="foo,baz,quux /a:${test.assemblies} xyzzy" dynamic="true"/>
<call target="do-unit-test"/>
</target>
<target name="do-unit-test-coverage">
<echo message="test.executable = ${test.executable}, test.args = ${test.args}" />
</target>
<target name="do-unit-test">
<property name="test.assemblies" value="MyProject.dll"/>
<call target="do-unit-test-coverage" />
</target>
</project>
user#host:/tmp/anttest$ nant run-unit-tests
[...snip...]
run-unit-tests:
do-unit-test:
do-unit-test-coverage:
[echo] test.executable = tools\nunit\nunit-console.exe, test.args = foo bar -assembly MyProject.dll baz
BUILD SUCCEEDED
Total time: 0 seconds.
user#host:/tmp/anttest$ nant -D:teamcity.dotnet.nunitlauncher=nunitlauncher run-unit-tests-teamcity
[...snip...]
run-unit-tests-teamcity:
do-unit-test:
do-unit-test-coverage:
[echo] test.executable = nunitlauncher, test.args = foo,baz,quux /a:MyProject.dll xyzzy
BUILD SUCCEEDED
Total time: 0 seconds.
If you really, really just need to know if you're running in TeamCity, then this should help:
<target name="run-unit-tests-teamcity">
<property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>
<property name="running.in.teamcity" value="true"/>
<call target="do-unit-tests"/>
</target>
I've managed to solve the problem. I don't know if it's the best solution but it is a solution.
I set a property called test.type and then use an if statement to determine which target it came from.
<target name="run-unit-tests">
<property name="test.executable" value="tools\nunit\nunit-console.exe"/>
<property name="test.type" value="unit-tests" />
<call target="do-unit-tests"/>
</target>
<target name="run-unit-tests-teamcity">
<property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>
<property name="test.type" value="unit-tests-teamcity" />
<call target="do-unit-tests"/>
</target>
<target name="do-unit-test">
<property name="test.assemblies" value="MyProject.dll">
<call target="do-unit-test-coverage" />
</target>
<target name="do-unit-test-coverage">
<if test="${test.type=='unit-tests'}">
<property name="test.args" value="${test.assemblies} ..."/>
</if>
<if test="${test.type=='unit-tests-teamcity'}">
<property name="test.args" value="... ${test.assemblies}"/>
</if>
<ncover <!--snip -->
commandLineArgs="${test.args}"
<!--snip-->
</ncover>
</target>