question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestNG swallowing an IllegalArgumentException when DataProivder mismatches Test

See original GitHub issue

I have hit an issue where TestNG will hide a IllegalArgumentException when it throws one and the exception is marked as an expectedExceptions. This happens when TestNG is matching a dataProvider up to a test’s parameters, the data does not match up, and IllegalArgumentException is an expectedExceptions. An example of this is:

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class swallowedException
{
    @DataProvider
    public Object[][] testInts()
    {
        return new Object[][] {
                new Object[] {new Integer(4) },
                new Object[] {new Integer(8) },
                new Object[] {new Integer(12) } };
    }

    @Test(dataProvider = "testInts", expectedExceptions = IllegalArgumentException.class)
    public void theTest(String aString)
    {
        System.out.println("An item is \"" + aString + "\"");
    }
}

In this case TestNG will throw an IllegalArgumentException when trying to match up the Integer from the dataProvider to the String parameter in the test (which is as expected.) However, because IllegalArgumentException is marked as an expected exception in the test, TestNG is marking this test as PASSED (all 3 times) when in fact it was not run. For this example, if you remove the expectedExceptions annotation, TestNG marks the test as FAILED with the exception thrown by TestNG. The actual code being tested was expected to throw an IllegalArgumentException, was not being tested, and was marked by TestNG as PASSED.

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dimo414commented, May 3, 2016

@juherr I can’t promise anything, but I’ll certainly send a pull request if I have time.

0reactions
dimo414commented, Dec 9, 2016

Looks like this is fixed, thanks! I think f3e44d60bf67 was the fix (since it added the “Data provider mismatch” message in DataProviderMethodMatcher). This should probably be called out in the release notes (if it’s not already) since it’ll cause some “passing” tests to start failing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TestNG : Go to the next test after one iteration of dataProvider
When you inject data into the test method using @DataProvider TestNg will run this method in a row as many times as many...
Read more >
Detecting Sample Ratio Mismatch in your A/B Tests
When an experiment is paused and we look to investigate the statistics of a given test we often ponder on our findings.
Read more >
Fail instead of Skip a Test when TestNG's DataProvider throws ...
A quite tricky problem I have been faced with was a failing TestNG-DataProvider which threw an exception because of corrupt test data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found