I'm trying to create an ant script to run pitest to be able to automate my mutation testing. I am getting the error:
Could not find or load main class org.pitest.mutationtest.commandline.MutationCoverageReport
This is my MutationTest.xml ant script
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="mutationCoverage" name="PhoneBook">
<property name="ECLIPSE_HOME" value="C:/Program Files/eclipse/"/>
<path id="JUnit 4.libraryclasspath">
<pathelement location="${ECLIPSE_HOME}plugins/org.junit_4.11.0.v201303080030/junit.jar"/>
<pathelement location="${ECLIPSE_HOME}plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>
</path>
<path id="PhoneBook.classpath">
<pathelement location="bin"/>
<path refid="JUnit 4.libraryclasspath"/>
</path>
<path id="pit.path">
<pathelement location="lib/pitest-1.1.4.jar" />
<pathelement location="lib/pitest-ant-1.1.4.jar" />
</path>
<taskdef name="pitest" classname="org.pitest.ant.PitestTask" classpathref="pit.path" />
<target name="mutationCoverage">
<pitest
pitClasspath="PhoneBook.path"
classPath="PhoneBook.path"
targetClasses="pbook.*"
targetTests="pbook.*"
reportDir="MutationReports"
sourceDir="src"/>
</target>
</project>
What is causing this error, and how can I fix it?
Edit: I changed pitClasspath="PhoneBook.path" to pitClasspath="pit.path" and now I have a new error:
[pitest] Exception in thread "main" org.pitest.util.PitError: Unable to load class content for org.pitest.boot.HotSwapAgent
[pitest] Please copy and paste the information and the complete stacktrace below when reporting an issue
[pitest] VM : Java HotSpot(TM) 64-Bit Server VM
[pitest] Vendor : Oracle Corporation
[pitest] Version : 25.25-b02
[pitest] Uptime : 370
[pitest] Input ->
[pitest] BootClassPathSupported : true
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.classBytes(JarCreatingJarFinder.java:124)
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.addClass(JarCreatingJarFinder.java:113)
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.createJarFromClassPathResources(JarCreatingJarFinder.java:98)
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.createJar(JarCreatingJarFinder.java:74)
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.getJarLocation(JarCreatingJarFinder.java:63)
[pitest] at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:70)
[pitest] at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:43)
[pitest] at org.pitest.mutationtest.commandline.MutationCoverageReport.runReport(MutationCoverageReport.java:72)
[pitest] at org.pitest.mutationtest.commandline.MutationCoverageReport.main(MutationCoverageReport.java:43)
I don't know if that is better or worse, but hopefully it will be helpful in finding the problem.
A working example of a pitest ant build is provided at
https://github.com/hcoles/pitest-ant-example
I suggest you start with this and edit it until you have a working build for your codebase.
One difference I can see is that you have not included junit on the pitest path.
Your build looks a little strange as it seems to be tied to eclipse. If you are running from the IDE why not use the eclipse plugin?
Also, if you are not tied to Ant, you might want to consider maven as an alternative.
I believe much of your problem is that you're trying to use the Eclipse generated build.xml file, which doesn't contain the mutation testing target, and the target which you've added to remedy this has some errors.
I would suggest starting with the project here and attempting to understand how it works, and then changing their build.xml file to fit your needs.
However if this doesn't work, judging from your other question, the following build.xml should work if:
You divide your files into two source directories "src" and "test"
Both src and test folders contain the package "pbook"
You change the name of your tests to end in "Test" rather than begin in it
<?xml version="1.0" encoding="UTF-8"?>
<project name="Phonebook">
<property name="classOutputDir" value="build" />
<!-- classpath for pitest and any plugins -->
<path id="pitest.path">
<!-- must currently include the test library on the tool classpath. this will be fixed in a future version-->
<pathelement location="lib/junit-4.9.jar" />
<pathelement location="lib/pitest-0.33.jar" />
<pathelement location="lib/pitest-ant-0.33.jar" />
</path>
<taskdef name="pitest" classname="org.pitest.ant.PitestTask" classpathref="pitest.path" />
<target name="clean">
<delete dir="${classOutputDir}" />
</target>
<target name="compile" depends="clean">
<mkdir dir="${classOutputDir}/classes" />
<!-- Essential that line numbers and filenames are included in order for PIT to work -->
<javac srcdir="src" includeantruntime="false" debug="true" debuglevel="source,lines" destdir="${classOutputDir}/classes" />
</target>
<!-- classpath for compiling and testing the code. Note it does not include pitest and it's dependencies -->
<path id="test.path">
<pathelement location="${classOutputDir}/classes" />
<pathelement location="${classOutputDir}/test-classes" />
<pathelement location="lib/junit-4.9.jar" />
</path>
<target name="test" depends="compile">
<mkdir dir="${classOutputDir}/test-result" />
<mkdir dir="${classOutputDir}/test-classes" />
<javac includeantruntime="false" srcdir="test" destdir="${classOutputDir}/test-classes">
<classpath refid="test.path" />
</javac>
<junit>
<classpath refid="test.path" />
<batchtest todir="${classOutputDir}/test-result">
<!-- set test classes -->
<fileset dir="test">
<include name="**/*Test.java" />
</fileset>
<formatter type="xml" />
</batchtest>
</junit>
<junitreport todir="${classOutputDir}/test-result">
<fileset dir="${classOutputDir}/test-result">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${classOutputDir}/test-result/report" />
</junitreport>
</target>
<!-- run pitest. note that the filters for tests and classes refer to package/class names, not source file named -->
<target name="pit" depends="test">
<path id="mutation.path">
<path refid="pitest.path"/>
<path refid="test.path"/>
</path>
<pitest pitClasspath="pitest.path" threads="2" classPath="mutation.path" targetTests="pbook.*" targetClasses="pbook.*" reportDir="pitReports" sourceDir="src" />
</target>
</project>
Related
When attempting to run tests with JUnit 5 via the Ant junitlauncher task, I receive the following message:
[junitlauncher] WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
[junitlauncher] java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
How do I fix this so that my tests can run?
The answers to similar questions (e.g. this one) mainly say to make sure that hamcrest is on the classpath. I've verified using the print-test-classpath target (see build file below for details) that the classpath I'm using to run my tests includes hamcrest-2.1.jar. Furthermore, the tests compile without any issue.
Here are the sections of my build.xml I think are relevant:
<path id="lib-only-class.path">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<path id="class.path">
<path refid="lib-only-class.path"/>
<pathelement location="${classes.dir}"/>
</path>
<path id="test-class.path">
<path refid="class.path"/>
<pathelement location="${test-classes.dir}"/>
</path>
<target name="compile-tests" depends="compile">
<mkdir dir="${test-classes.dir}"/>
<javac srcdir="${test.dir}" destdir="${test-classes.dir}"
includeantruntime="no" classpathref="class.path"/>
</target>
<target name="test" depends="compile-tests">
<junitlauncher printsummary="yes">
<classpath refid="test-class.path"/>
<testclasses>
<fileset dir="${test-classes.dir}"/>
</testclasses>
</junitlauncher>
</target>
<target name="print-test-classpath">
<pathconvert property="classpathInName" refid="test-class.path"/>
<echo>Classpath is ${classpathInName}</echo>
</target>
I have a simple Junit test that keeps throwing a java.lang.ClassNotFoundException. Below is the folder structure
project:
|__build.xml
|__hamcrest-core-1.3.jar
|__junit.jar
|__src:
| |__Simple.java
|__bin:
|__Simple.class
Simple.java
import org.junit.Test;
public class Simple {
#Test
public void check() {
assert (true);
}
}
build.xml
<?xml version="1.0"?>
<project name="Ant-Test" default="main">
<target name="main">
<junit printsummary="yes" fork="yes" haltonfailure="no">
<classpath path="hamcrest-core-1.3.jar" />
<classpath path="junit.jar" />
<formatter type="plain" />
<batchtest fork="yes" todir="docs">
<fileset dir="bin">
<include name="**.class" />
</fileset>
</batchtest>
</junit>
</target>
</project>
Error:
Testsuite: Simple
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
Caused an ERROR
Simple
java.lang.ClassNotFoundException: Simple
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:292)
Please help me find the reason for why this error occurs. The script is able to find the test case but at the end an error is thrown.
Add the required resources to the test classpath:
<pathelement path="${classpath}"/>
that in your case will be:
<classpath>
<pathelement path="bin" />
<pathelement location="hamcrest-core-1.3.jar" />
<pathelement location="junit.jar" />
</classpath>
As explained here
To make things a bit more flexible, we add one extra feature, which makes it possible to match multiple directory levels. This can be used to match a complete directory tree, or a file anywhere in the directory tree. To do this, "" must be used as the name of a directory. When ** is used as the name of a directory in the pattern, it matches zero or more directories. For example: /test/ matches all files/directories under /test/, such as /test/x.java, or /test/foo/bar/xyz.html, but not /xyz.xml.
'**' must be used to match folders, and not as wildcard to select partial file names.
Change your ant with:
<fileset dir="bin">
<include name="*.class" />
</fileset>
If you want to select all '*.class' files under every directory under bin/ then use:
<fileset dir="bin">
<include name="**/*.class" />
</fileset>
You will have now an error cause by the test report writer. You need to create a docs folder and everything will work.
The final build.xml will be:
<?xml version="1.0"?>
<project name="Ant-Test" default="main">
<target name="main">
<mkdir dir="docs"/>
<junit printsummary="yes" fork="yes" haltonfailure="no">
<classpath location="hamcrest-core-1.3.jar" />
<classpath location="junit-4.12.jar" />
<classpath path="bin/" />
<formatter type="plain" />
<batchtest fork="yes" todir="docs">
<fileset dir="bin">
<include name="**/*.class" />
</fileset>
</batchtest>
</junit>
</target>
</project>
Please try with following fileset tag:
<fileset dir="bin">
<include name="**.java" />
</fileset>
I'm not sure if junit task admits more than one classpath node, but anyway, it is a good practice to have just one. Moreover, your classpath is missing the classes directory. The right declaration would be like this:
<junit ...>
<classpath>
<pathelement path="bin"/>
<pathelement location="hamcrest-core-1.3.jar"/>
<pathelement location="junit.jar"/>
</classpath>
...
</junit>
On Eclipse I create war files by using ant.
The issue is that in the war file isn't included the right mypropfile.properties.
The file is properly copied, but also if I use <eclipse.refreshLocal resource="projectdir" depth="infinite"/> the old file is included. I have to refresh manually the project.
For Ant I use the "Run in the same JRE as the workspace" option.
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" basedir=".">
<description>
My Project
</description>
<property name="workspace.dir" value="${basedir}/../../"/>
<property name="src" value="${basedir}/../src"/>
<property name="build" value="${basedir}/../build"/>
<property name="build.classes" value="${basedir}/../build/classes"/>
<property name="lib.dir" value="${basedir}/WEB-INF/lib"/>
<property name="web.dir" value="${basedir}/WEB-INF"/>
<property environment="env"/>
<property name="real.dir" value="${basedir}/real"/>
<property name="real2.dir" value="${basedir}/real2"/>
<path id="classpath.server">
<fileset dir="${env.CATALINA_HOME}/lib" includes="*.jar"/>
<pathelement path="${build.classes}"/>
</path>
<path id="classpath.app">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
<target name="refreshResource" if="eclipse.refreshLocal">
<eclipse.refreshLocal resource="projectdir" depth="infinite"/>
</target>
<target name="clean">
<delete dir="${build}/classes"/>
<delete dir="${build}"/>
</target>
<target name="init" depends="clean, refreshResource">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${build}/classes"/>
</target>
<target name="compile" depends="init">
<javac encoding="UTF8" srcdir="${src}" destdir="${build}/classes" includeantruntime="false">
<compilerarg value="-Xlint:unchecked"/>
<classpath>
<path refid="classpath.server.bin"/>
</classpath>
<classpath>
<path refid="classpath.server"/>
</classpath>
<classpath>
<path refid="classpath.app"/>
<fileset dir="${lib.dir}" includes="*.jar"/>
</classpath>
</javac>
</target>
<target name="deleteConfig">
<delete file="${src}/mypropfile.properties"/>
</target>
<target name="real" depends="deleteConfig">
<copy file="${real.dir}/realprop.properties" tofile="${src}/mypropfile.properties"/>
</target>
<target name="real2" depends="deleteConfig">
<copy file="${real2.dir}/real2prop.properties" tofile="${src}/mypropfile.properties"/>
</target>
<target name="war-real" depends="real, compile">
<input message="Warname (without .war):" addproperty="warname"/>
<war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
<fileset dir="${basedir}">
<include name="**/*.*"/>
</fileset>
<classes dir="${build.classes}"/>
</war>
</target>
<target name="war-real2" depends="real2, compile">
<input message="Warname (without .war):" addproperty="warname"/>
<war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
<fileset dir="${basedir}">
<include name="**/*.*"/>
</fileset>
<classes dir="${build.classes}"/>
</war>
</target>
EDIT
The target clean was wrong, so I've corrected it, but now build failed with error
BUILD FAILED ... Reference classpath.server.bin not found.
Ant doesn't care if Eclipse has refreshed the file or not. eclipse.refreshLocal is only relevant for editors and compilers inside of the IDE.
When you run the Ant build.xml, Ant copies the file in question in the real target into the source folder and compile copies it into ${build}/classes (at least it should do that). So before you create the WAR, you must make sure the compile step has done its work (i.e. look into each file to make sure that a change is visible in each copy).
What worries my is that you use different ways to access the classes:
${build}/classes
${build.classes}
${basedir}/../build/classes
So the first step should be to define a single way to locate the folder and then use this pattern everywhere.
If that doesn't solve your problem, you need to make sure Ant notices that the file has changed. Old filesystems like FAT support only timestamps which have second resolution. If you use an USB stick for your sources, it's possible to change the file and run Ant so fast that Ant thinks the file hasn't changed.
Lastly, you need to check your classpath. If one of the JAR dependencies also contains a file called mypropfile.properties, then Java resource loading can find either version.
This and other problems made me use a different solution to configure WAR files: I pass a system property with the absolute path of the config file. That way, the WAR file doesn't change when the config changes and I have full control over which config file is loaded.
After doing so many trial and errors on "How ANT tag works ?", I decided to write my own custom build xml file for the testcases that was written in Java and integrated with JUnit. Unfortunately my build script failing with "ClassNotFoundException". And i can see the log in generated HTML file that you get to see after running ant build script.
Please see below
<project name="WebServices integrated with JUnit and generating report with ANT" default="test" basedir="." >
<description> REST Services integration with JUnit </description>
<!-- set global properties for this build -->
<property name="project_name" value="junit"/>
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="dist" location="dist"/>
<property name="lib" location="${user.home}/My Documents/Mr/jars"/>
<property name="reports" location="reports"/>
<!-- the names of various distributable files. NOTE: Generating distribution file "target" is not used here-->
<!-- Delete the ${build} and ${dist} directory trees -->
<target name="clean" description="clean up">
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
</target>
<!-- Top level targets -->
<target name="compile" depends="init" description="compile the source code">
<javac srcdir="${src}" destdir="${build}">
<classpath>
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<!-- run your tests -->
<target name="run-tests" depends="compile" description="run your test suite">
<junit printsummary="yes" haltonfailure="no" showoutput="yes">
<classpath>
<pathelement path="${build}"/>
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${src}/Test/Services" >
<exclude name="MyFile.java"/>
<include name="**/*.java"/> // <------ IMP***: Here I am saying include .java files that are based at "${src}/Test/Services".
</fileset>
</batchtest>
</junit>
</target>
<!-- generate report on tests -->
<target name="test" depends="run-tests">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${reports}/html/"/>
</junitreport>
</target>
<target name="init" depends="clean" description="initialize the build envrionment">
<!--create the time stamp -->
<tstamp/>
<!-- Create directory structure -->
<mkdir dir="${build}"/> //<----dir for class files
<mkdir dir="${lib}"/> //<----dir for all my libraries
<mkdir dir="${dist}/lib"/> //<----not used
<mkdir dir="${reports}"/>
<mkdir dir="${reports}/raw/"/>
<mkdir dir="${reports}/html/"/> //<---- it will have output reports
</target>
<target name="all" depends="clean,test">
</target>
And I guessed ANT build will pick all the source files (.java) and then it will look for all the class files that are based in build folder and started running them, but then I see "classNotFoundException" in HTML report. please see below log :
CLASS : "getlieninfo"
> java.lang.ClassNotFoundException: getlieninfo
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
After a while I changed .java to .class in "include" tag of my target called "run-tests" . The reason for doing this is, I thought ANT is not able to look ".java" files in source folder (src/Test/Services) hence I changed to ".class" and then modified dir attribute value in "fileset" tag to "build" so it may easily look for ".class" in BUILD folder where I will have compiled files stored. But none of my trial and error succeeded and ended up with same "classNotFoundException".
I am not sure what went wrong , can someone help me, please?
OK guys, Thanks for what ever little help I got from you. I found the problem.
I had to change include tag attribute name to "name=/Test/Services/**/*.java"/>" and modify dir attribute in fileset tag to "${src}" ; see below :
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${src}" >
<exclude name="MyFile.java"/>
<include name="/Test/Services/**/*.java"/> //<----this where i had to modify.
NOTE: Under src directory I have Test/Services folder which has source ".java" files in it.
</fileset>
</batchtest>
It solved my problem, but then i don't understand why ant build was not able to identify source files when i gave dir attribute as dir="${src}/Test/Services" that is in fileset tag
and kept name attribute in include tag as name="**/*.java" . My understanding is fileset tag should build the path for for the given dir path , once the path is build include tag will include or look for mentioned source files i.e. ".java" .
We are in the process of upgrading from BEA Weblogic and ALSB to Oracle Weblogic and OSB 10.3.4. One of the things that are failing is our ant task for compiling jsp pages for our WAR.
This is the part of the ant build file that is failing now:
<!-- Compile the JSP files -->
<path id="workshop.classpath">
<fileset dir="${env.WL_HOME}/workshop/lib">
<include name="*.jar"/>
</fileset>
<pathelement location="${env.ORACLE_HOME}/tools/eclipse_pkgs/1.1/pkgs/eclipse/plugins/com.bea.workshop.wls.ant_1.0.20.200802230117/workshop-wls-antlib.jar"/>
</path>
<taskdef
name="jspc"
classname="com.bea.workshop.wls.antlib.tasks.JspcTask" classpathref="workshop.classpath"/>
<path id="jspc.classpath">
<path refid="java.classpath"/>
<pathelement location="${env.ORACLE_HOME}/jdk150_11/lib/tools.jar" />
<pathelement location="${env.WL_HOME}/server/lib/weblogic.jar" />
</path>
<jspc source="#{war.staging.dir}" classpathref="jspc.classpath" failonerror="true" />
It cannot resolve the jar file with JspcTask since Workshop does not exist in 10.3.4. So the question is, how should this ant script be changed to work under 10.3.4?
The taskdef with the classname below works with WLS 10.3.3
<taskdef name="jspc"
classname="weblogic.ant.taskdefs.j2ee.Jspc" classpath="{ORACLE_MIDDLEWARE_HOME}\wlserver_10.3\server\lib\weblogic.jar">
</taskdef>
Check your weblogic.jar with winzip/winrar to see if the above classname exists in 10.3.4, and it should be fine.
Update:
You're getting the NullPointer because jspc does not allow a directory in the source attribute. It should be srcdir as per the docs, in fact I dont see a source attribute at all.
The jspc task is deprecated, so I think it's better you use the weblogic.appc like below
<target name="compileapp">
<java classname="weblogic.appc" fork="yes">
<arg line="-webapp ${src.gui}" />
<arg value="-compiler javac" />
<arg line="-classpath D:\Oracle\Middleware_Jdev11_1_1_4\wlserver_10.3\server\lib\weblogic.jar" />
<arg value="-depend" />
<arg value="-nowarn" />
</java>
</target>
Choose the attributes you want from this list, I havent tried this myself yet.