0

I have this bizarre situation with TestNG and hoping someone could help.

I'm trying to test for an IllegalArgumentException, essentially trying to block anything malicious that might end being passed into a setter on a model. The test that I have annotated with the expected exception compiles and runs fine, but it passes despite me not throwing any IllegalArgumentException in the code and I am at a loss as to why.

public class CarTest extends ModelTest {

@Test(expectedExceptions = InvalidArgumentException.class)
public void willThrowInvalidArgumentExceptionWhenSettingEngineToNull() throws InvalidArgumentException{
    Car car = new Car();
    car.setEngine(null);
}

}

This passes despite no thrown exception.

The model I am using is a groovy model so will have already a public setter for this property.

I have changed the model names but it shouldn't make any difference in this case.

Thank you in advance for your wisdom

rb20
  • 129
  • 2
  • 10

2 Answers2

0

It seems to me that car.setEngine(null); is throwing the exception. Remove the line and the test should fail or send a valid argument.

Pablo Miranda
  • 369
  • 1
  • 9
  • Thank you I will give that a go, however I've changed the expectedExceptions value to other exceptions and my test still passes, which makes me think I'm just missing something but I've checked and all the classes I've imported are correct, it's driving me nuts! – rb20 Feb 21 '19 at 20:09
0

I have fixed this by deleting .idea folder in IntelliJ and reimporting my project using my pom.xml file, I can only assume TestNG had not been pulled in properly and that is why expectedExceptions was not fully recognised.

rb20
  • 129
  • 2
  • 10