Not able to get expected expections to work..

400 views
Skip to first unread message

Kartik Kumar

unread,
Sep 2, 2013, 1:50:45 AM9/2/13
to testng...@googlegroups.com

I have a test that is failing and I hope that some can help. The test attempts to verify that AssertionError is raised but TestNGException is raised instead.

The test in question:

 @Test(
      expectedExceptions = AssertionError.class,
      expectedExceptionsMessageRegExp =
          "expected not to match regex <a(.)> but was <ab>")
  public void testNotMatchesRegexFailure() throws Exception {
    try {
      System.out.println("################ \n Begin");
      TestNGAsserts.assertNotMatchesRegex("a(.)", "ab");
    } catch (AssertionError e) {
      Assert.assertEquals(
          "expected not to match regex <a(.)> but was <ab>",
          e.getMessage()); // This works.
      System.out.println("end test " + e + "\n##########");
      throw e;
    }
  }

The output when running an Maven via command line or in IntelliJ

################
 Begin
end test java.lang.AssertionError: expected not to match regex <a(.)> but was <ab>
##########

The stack trace is given below

Expected exception java.lang.AssertionError but got org.testng.TestException: The exception was thrown with the wrong message: expected "expected to match regex <a(.)> but was:null" but got "expected to match regex <a(.)> but was:null"

org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1459) 
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1222) 
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
at org.testng.TestRunner.privateRun(TestRunner.java:758) 
at org.testng.TestRunner.run(TestRunner.java:613) 
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) 
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87) 
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1137) 
at org.testng.TestNG.runSuitesLocally(TestNG.java:1062) 
at org.testng.TestNG.run(TestNG.java:974) 
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:70) 
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:158) 
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:98) 
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:111) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) 
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) 
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) 
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) 
1 lines not shown
Caused by The exception was thrown with the wrong message: expected "expected to match regex <a(.)> but was:null" but got "expected to match regex <a(.)> but was:null"

org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1452) 
at org.testng.internal.Invoker.invokeMethod(Invoker.java:740) 
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:883) 
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1208) 
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
at org.testng.TestRunner.privateRun(TestRunner.java:758) 
at org.testng.TestRunner.run(TestRunner.java:613) 
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) 
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87) 
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1137) 
at org.testng.TestNG.runSuitesLocally(TestNG.java:1062) 
at org.testng.TestNG.run(TestNG.java:974) 
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:70) 
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:158) 
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:98) 
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:111) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) 
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) 
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) 
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) 
1 lines not shown
Caused by expected to match regex <a(.)> but was:null

org.testng.Assert.fail(Assert.java:89) 
at com.kartik.common.testing.testng.TestNGAsserts.failWithMessage(TestNGAsserts.java:163) 
at com.kartik.common.testing.testng.TestNGAsserts.failNotMatches(TestNGAsserts.java:173) 
at com.kartik.common.testing.testng.TestNGAsserts.assertMatchesRegex(TestNGAsserts.java:49) 
at com.kartik.common.testing.testng.TestNGAsserts.assertMatchesRegex(TestNGAsserts.java:60) 
at com.kartik.common.testing.testng.TestNGAssertsTest.testMatchesRegexNullFailure(TestNGAssertsTest.java:81) 
35 lines not shown

Please advise.

Thanks,

Kartik Kumar

Krishnan Mahadevan

unread,
Sep 2, 2013, 5:03:53 AM9/2/13
to testng...@googlegroups.com
Can you please elaborate as to what exactly are you trying to accomplish here ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.

krishn...@gmail.com

unread,
Sep 2, 2013, 6:36:13 PM9/2/13
to testng...@googlegroups.com
I wrote additional testng assertions. Should those assertions fail, then AssertionError is thrown. In my unit tests, I check if AssertionError is thrown.

The tests work if I use try/catch verify model but when I use TestNG's expected exceptions attribute

Krishnan Mahadevan

unread,
Sep 2, 2013, 10:05:32 PM9/2/13
to testng...@googlegroups.com
If you wrote additional assertions then those assertions should still fail a test by itself. Why do you need a try.. Catch here? Am still not getting it. What exactly are you gaining by checking if assertion error was thrown when TestNG by itself looks for that as well ?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.


--

Kartik Kumar

unread,
Sep 3, 2013, 1:11:50 AM9/3/13
to testng...@googlegroups.com

I don't need it. I wanted to check if the AssertionError is raised. It is raised by the function. But AssertionError is replaced by TestNGException. The try/catch block merely verifies that AssertionError is raised.

> You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/k1ChDHjXG-Q/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.

krishn...@gmail.com

unread,
Sep 7, 2013, 3:18:08 AM9/7/13
to testng...@googlegroups.com
Any assistance would be appreciated.


On Monday, September 2, 2013 10:11:50 PM UTC-7, Kartik Kumar wrote:

I don't need it. I wanted to check if the AssertionError is raised. It is raised by the function. But AssertionError is replaced by TestNGException. The try/catch block merely verifies that AssertionError is raised.

>> To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.


>>
>> To post to this group, send email to testng...@googlegroups.com.
>> Visit this group at http://groups.google.com/group/testng-users.
>> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
> --
> Thanks & Regards
> Krishnan Mahadevan
>
> "All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
> My Scribblings @ http://wakened-cognition.blogspot.com/
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/k1ChDHjXG-Q/unsubscribe.

> To unsubscribe from this group and all its topics, send an email to testng-users+unsubscribe@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages