Error creating object factory in TestNG - ant

I have a problem with TestNG. When I run tests in Eclipse everything it's OK, but when I run test in ANT i get errors:
TEST-RUN:
[testng] [TestNG] [Error]
[testng] Error creating object factory: class SomeClassTest
[testng] The tests failed.
In test I use:
#ObjectFactory
public IObjectFactory getObjectFactory() {
return new PowerMockObjectFactory();
}

You might have to use the 'objectFactory' attribute in the testng ant task, something like objectFactory="org.powermock.modules.testng.PowerMockObjectFactory". See https://groups.google.com/forum/#!topic/testng-dev/h68SIRxbeNI for more information. Hope that helps?

Related

Ant build fails unnecessarily when JaCoCo determines there are no tests worth running

For a continuous integration build, we use JcCoCo to minimise the number of tests to be run. However, on some commits it determines that there are no tests worth running at all. For example, if only an image was changed.
Here is a snippet from the build.xml:
<fileset id="int.tests" dir="${build.inttest.source}/java">
<include name="**/*Test.java"/>
</fileset>
<taskdef name="testng" classname="org.testng.TestNGAntTask"
classpath="${build.jars.test}/testng.jar"/>
<jacoco:coverage destfile="./inttest-jacoco.exec">
<testng outputDir="./reports/intTest" failureproperty="testNGFailed" haltonfailure="false"
verbose="2" workingDir="${build.dir}" classfilesetref="int.tests">
<classpath>
<path refid="build.inttest.classpath"/>
</classpath>
</testng>
</jacoco:coverage>
When no tests are run, property testNGFailed is set to true and the build subsequently fails.
The logging in this scenario looks like this:
13:16:49,116 INFO - [testng] ===============================================
13:16:49,116 INFO - [testng] Ant suite
13:16:49,116 INFO - [testng] Total tests run: 0, Failures: 0, Skips: 0
13:16:49,116 INFO - [testng] ===============================================
13:16:49,116 INFO -
13:16:49,191 WARN - [testng] [TestNG] No tests found. Nothing was run
How can I make the build pass when there are no test to be run, but fail when any test fails?
Can I get Jacoco to always run at least one test?
Can I get TestNG to only set the failureproperty when a test has failed?
First of all - JaCoCo does not execute your tests! Tests are executed by TestNG or JUnit or whatever testing framework - JaCoCo just collects coverage information from this execution.
According to http://testng.org/doc/ant.html :
classfilesetref - A reference to a ResourceCollection containing the test classes to be run
whereas in your case it points on directory containing .java files, so seems logical that TestNG can't find tests.

Junit test error with Ant, no message, no reason given

I'm trying to use Junit test with ant.
Since I don't really know how to write Junit test without eclipse, I'll just start with something completely trivial I found online. This should always pass.
Test1.java
import junit.framework.*;
public class Test1 extends TestCase {
#Test
public void testOne()
{
System.out.println("Heyy there I'm a test!");
assertTrue( "TestExample", true );
}
}
I add to my build.xml these...
49 <target name="junit" depends="jar">
50 <junit printsummary="withOutAndErr" fork="yes" showoutput="yes">
51 <classpath refid="application"/>
52 <test name="test.Test1"/>
53 </junit>
54 </target>
So now when I run "ant junit" I'm getting
junit:
[junit] Running test.Test1
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
So... that doesn't tell me anything... what was wrong? how to fix it? nothing.
Anyone knows what's up?
I can reproduce it. I was expecting junit to give some more verbose message, like Class test.Test1 not found. But no, it just fails. It means the class is not in the classpath. Open your jar file (with archivizer like 7zip to confirm that).
And no surprise, as your class does not compile :) Add the following:
package test;
import org.junit.Test;
and your first test should pass.
#jarekczek helped me huge by letting me know that Junit is not some external thing to be compiled separately to execute but it needs to be compiled into the jar with your other source files.
The problem was that I put my test/ directory outside of my src/ directory, and when ant compiles the java files in src/ it did not compile the file Test1.java into Test1.class
I moved the test/ folder into the src/ folder, and now I got more informative error messages from the javac compiler, and was able to fix it. Thanks so much!

Suddenly can't run TestNG tests from ant ([testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException)

I am using ant and TestNG 6.1.1, and I had my tests working just fine yesterday. Today when I try to run the tests I get a NoClassDefFoundError involving com/beust/jcommander/ParamException. Here is the output:
[copy] Copying 1 file to /Users/djohnson/src/webapp/components/build/tmp
[testng] Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
[testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
[testng] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[testng] at java.security.AccessController.doPrivileged(Native Method)
[testng] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[testng] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[testng] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[testng] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[testng] The tests failed.
Here is the .xml file I'm using:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Single Method Suite">
<test name="Single Method Test">
<classes>
<class name="DhcpTest">
<methods>
<exclude name=".*" />
<include name="setGetEnabledTest" />
</methods>
</class>
</classes>
</test>
</suite>
I've also specified the complete path for the class with no luck. I've checked the classpath and everything seems fine, does anyone have any ideas on this?
If you're using ant, JCommander should be inside testng.jar, so I'm thinking your testng.jar file might be corrupt.
If your using Maven try adding this to your pom.xml
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.30</version>
</dependency>
jcommander.jar (e.g. jcommander-1.29.jar) should be present on fileset directory which is mentioned at build.xml
You can download it from: http://mvnrepository.com/artifact/com.beust/jcommander/1.27
OR,
JCommander should be inside testng.jar during using ant. Your testng.jar file might be corrupted, so please replace old testng.jar by new one.
May be testng.jar is not in classpath.
I had this same problem with a non-Maven project. I retrieved the testng jar from Maven Central, but it didn't contain all of TestNG's dependencies. Downloading the jar directly from the TestNG site resolved the issue, as it contains the dependencies.
I should have paid more attention to the directions on TestNG's download page...
The latest version of TestNG can be downloaded from Maven Central or here for ant users.
The problem here is that certain versions of the testNG jar do not have the jcommander jar code inside of them. As a result, you have to add the classpath to the Jcommander jar separately.
Adding the Jcommander jar to your classpath when you run it will fix the problem, hopefully
My ant start working when I have downloaded jcommandar jar from
Maven repository and added it in my jar file
But This problem
Starts coming with testng 6.8.5 version

Debugging TestNG configuration failures

I'm running TestNG from Ant. I'm using my own test listeners. I'm refactoring the code and once a while I got
[testng] Total tests run: 7, Failures: 0, Skips: 7
[testng] Configuration Failures: 1, Skips: 2
What will be the best approach to fix configuration failures ?
The HTML reports will tell you which configuration methods failed.

Can JUnit Ant task report ignored tests?

We're currently using JUnit 4.4 and Ant 1.7.1 for our builds. I have some test cases that are #Ignored. When I run them in Eclipse, the JUnit test runner reports them as ignored. I would like to see them listed in the XML output from Ant (so I can report on them), but they do not seem to be there.
Does anyone have this working? Is there a switch to turn them on? An upgrade I need to do?
It looks like this is a known Ant issue/bug.
This thread talks about the same issue, but it provides some additional information: you can get data on ignored tests when running the tests using maven surefire, and hudson is able to display that data.
http://jenkins.361315.n4.nabble.com/Is-it-possible-to-show-Ignore-JUnit-tests-td1565288.html
A fix for this issue has now been applied to the head of Ant core, scheduled for release as part of the upcoming version of Ant 1.9.0.
It should be possible to try this fix locally be replacing ant-junit.jar in your Ant distribution's lib directory with the version from the nightly builds, or by running the full nightly Ant distribution, or by building the Ant sources directly. Since the Ant team are currently voting on preparing a new release it may just be worth waiting for 1.9.0 to be officially packaged and pushed out for download.
Just tried Ant 1.9.0 with JUnit 4.11. If you use <junit printsummary="on"> you'll get output like:
[junit] Running com.example.IgnoredTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.01
[junit] Running com.example.PassingTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01
[junit] Running com.example.FailingTest
[junit] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.01
I think it'd be preferrable if we could get output like this with printsummary=off:
[junit] Test com.example.IgnoredTest SKIPPED
[junit] Test com.example.FailingTest FAILED
but it seems the more verbose output above is the best we can do, unless I'm missing some obscure trick with one of the junit task arguments.

Resources