Closed Pr0methean closed 4 years ago
@Pr0methean - I looked at the test that you shared and created a similar test which looks like below
import java.io.File;
import org.testng.Assert;
import org.testng.TestNG;
import org.testng.annotations.Test;
public class ExampleTestCase {
@Test(timeOut = 1000, expectedExceptions = IllegalArgumentException.class)
public void testMethod() {
String version = new File(TestNG.class.getProtectionDomain().getCodeSource().getLocation().getFile()).getParentFile().getName();
Assert.assertEquals(version, "7.1.0");
throw new IllegalArgumentException("foo");
}
}
I am not able to simulate the problem with this sample.
I am not able to run the test from your codebase, because despite having only JDK8, for some reason IntelliJ keeps believing that I have JDK9 and keeps giving me some error.
The test passes when I run it in IntelliJ IDEA; this bug may be specific to the Maven runner, or IDEA may be ignoring the pom.xml and using an old version of TestNG. (NB: I'm using JDK11 in IDEA.)
Ok.. I think I got the problem.
Here's a sample that can be used to reproduce this issue
import org.testng.annotations.Test;
public class ExampleTestCase {
@Test(timeOut = 1000, expectedExceptions = IllegalArgumentException.class)
public void testMethod() {
throw new IllegalArgumentException("foo");
}
}
import java.util.Collections;
import org.testng.Assert;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlSuite.ParallelMode;
import org.testng.xml.XmlTest;
public class IssueTest {
@Test
public void testMethod() {
TestNG testng = new TestNG();
XmlSuite xmlSuite = new XmlSuite();
xmlSuite.setName("main");
xmlSuite.setParallel(ParallelMode.METHODS);
XmlTest xmlTest = new XmlTest(xmlSuite);
xmlTest.setName("prngs");
xmlTest.setXmlClasses(Collections.singletonList(new XmlClass(ExampleTestCase.class.getName())));
testng.setXmlSuites(Collections.singletonList(xmlSuite));
TestListenerAdapter listener = new TestListenerAdapter();
testng.addListener(listener);
testng.run();
Assert.assertTrue(listener.getFailedTests().isEmpty());
}
}
@Pr0methean - The issue is reproducible when you run a testng suite that looks like below
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Main" parallel="methods">
<test name="PRNGs">
<classes>
<class name="io.github.pr0methean.betterrandom.util.BinaryUtilsTest">
<methods>
<include name="testConvertWrongNumberOfBytesToInts"/>
</methods>
</class>
</classes>
</test>
</suite>
The code path seems to be linked to enabling parallel execution, of a test method that has a time out defined and also has an expected exception.
That would explain it; all the tests in BinaryUtilsTest are of static methods that are pure functions, so of course I let them run in parallel!
TestNG Version
7.1.0
Expected behavior
When the method under test throws an IllegalArgumentException, a test with
expectedExceptions = IllegalArgumentException.class
passes.Actual behavior
The test fails with:
Is the issue reproductible on runner?
Test case sample
Test: https://github.com/Pr0methean/BetterRandom/blob/6df10c5b6e8ec24dea4275bf2b487dd9aecdc369/betterrandom/src/test/java/io/github/pr0methean/betterrandom/util/BinaryUtilsTest.java#L93 Code under test throws the exception here: https://github.com/Pr0methean/BetterRandom/blob/ca5deaff0497db33d7769bdf697af9852389f151/betterrandom/src/main/java/io/github/pr0methean/betterrandom/util/BinaryUtils.java#L104
(This version of the test passes on TestNG 7.0.0.)