IAssert.getMessage() is null

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

IAssert.getMessage() is null

shreejitnair18
Hi,

As part of overriding the onAssertSuccess to get some custom log messages printed out I was not able to get the Iassert instance getMessage() that always seems to be throwing a null.

Logging class:

public class LoggingAssert extends Assertion {


private List<String> m_messages = Lists.newArrayList();

@Override

public void onBeforeAssert(IAssert a) {

m_messages.add("Test:" + a.getMessage());

}

@Override

public void onAssertSuccess(IAssert a) {

m_messages.add("Test:" + a.getMessage());

System.out.println(a.getMessage());

System.out.println("PASSED");

}

@Override

public void onAssertFailure(IAssert a) {

m_messages.add("Test:" + a.getMessage());

System.out.println("FAILED");

}

public List<String> getMessages() {

return m_messages;

}

}


Simple TestNG class:

public class NewTest {

private LoggingAssert m_assert = new LoggingAssert();


@Test

public void justCheck() {

m_assert.assertEquals("hello""hello");

}

}


I see the following in console:

null. // System.out.println(a.getMessage()); seems to be null

PASSED

PASSED: justCheck


I was looking to leverage this functionality to integrate with the Log4j logging framework in place.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: IAssert.getMessage() is null

Krishnan Mahadevan

The problem is in your test code. You are not invoking the 3 arg variant of assertEquals() method, wherein you pass in a string as well. Here’s a sample that shows this in action and the output as well.

 

import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

public class NewTest {
 
private LoggingAssert m_assert = new LoggingAssert();

 
@Test
 
public void justCheck() {
   
m_assert.assertEquals("hello", "hello", "Ensuring that both hellos are equal");
  }

 
@AfterClass
   
public void afterClass() {
     
m_assert.getMessages().forEach(System.err::println);
  }
}

 

Ensuring that both hellos are equal

PASSED

Test:Ensuring that both hellos are equal

Test:Ensuring that both hellos are equal

 

===============================================

Default Suite

Total tests run: 1, Failures: 0, Skips: 0

===============================================

 

 

Process finished with exit code 0

 

 

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/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

From: <[hidden email]> on behalf of Shreejit Nair <[hidden email]>
Reply-To: <[hidden email]>
Date: Thursday, May 17, 2018 at 5:25 PM
To: testng-users <[hidden email]>
Subject: [testng-users] IAssert.getMessage() is null

 

Hi,

 

As part of overriding the onAssertSuccess to get some custom log messages printed out I was not able to get the Iassert instance getMessage() that always seems to be throwing a null.

 

Logging class:

public class LoggingAssert extends Assertion {

 

       private List<String> m_messages = Lists.newArrayList();

       @Override

       public void onBeforeAssert(IAssert a) {

              m_messages.add("Test:" + a.getMessage());

       }

      

       @Override

       public void onAssertSuccess(IAssert a) {

              m_messages.add("Test:" + a.getMessage());

              System.out.println(a.getMessage());

              System.out.println("PASSED");

       }

       @Override

       public void onAssertFailure(IAssert a) {

              m_messages.add("Test:" + a.getMessage());

              System.out.println("FAILED");

       }

      

       public List<String> getMessages() {

              return m_messages;

       }

}

 

Simple TestNG class:

public class NewTest {

       private LoggingAssert m_assert = new LoggingAssert();

 

       @Test

       public void justCheck() {

              m_assert.assertEquals("hello""hello");

       }

}

 

I see the following in console:

null. // System.out.println(a.getMessage()); seems to be null

PASSED

PASSED: justCheck

 

I was looking to leverage this functionality to integrate with the Log4j logging framework in place.

--
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
[hidden email].
To post to this group, send email to
[hidden email].
Visit this group at
https://groups.google.com/group/testng-users.
For more options, visit
https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: IAssert.getMessage() is null

shreejitnair18
In reply to this post by shreejitnair18
Thanks a lot Krishnan.
I was not sure that the getMessage basically had to be supplied from a custom string message from our side.
Perfectly clear to me now. Thanks so much :)

On Thursday, May 17, 2018 at 7:55:00 AM UTC-4, Shreejit Nair wrote:
Hi,

As part of overriding the onAssertSuccess to get some custom log messages printed out I was not able to get the Iassert instance getMessage() that always seems to be throwing a null.

Logging class:

public class LoggingAssert extends Assertion {


private List<String> m_messages = Lists.newArrayList();

@Override

public void onBeforeAssert(IAssert a) {

m_messages.add("Test:" + a.getMessage());

}

@Override

public void onAssertSuccess(IAssert a) {

m_messages.add("Test:" + a.getMessage());

System.out.println(a.getMessage());

System.out.println("PASSED");

}

@Override

public void onAssertFailure(IAssert a) {

m_messages.add("Test:" + a.getMessage());

System.out.println("FAILED");

}

public List<String> getMessages() {

return m_messages;

}

}


Simple TestNG class:

public class NewTest {

private LoggingAssert m_assert = new LoggingAssert();


@Test

public void justCheck() {

m_assert.assertEquals("hello", "hello");

}

}


I see the following in console:

null. // System.out.println(a.getMessage()); seems to be null

PASSED

PASSED: justCheck


I was looking to leverage this functionality to integrate with the Log4j logging framework in place.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: IAssert.getMessage() is null

Sangitadas Nandi
In reply to this post by Krishnan Mahadevan

Hello ,
I am getting this issue in below code
java.lang.NullPointerException
    at RestuarantRegistration.registration(RestuarantRegistration.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
======================================================================
@Test()
            public void registration() throws InterruptedException{
        AdminURL openadmin = new AdminURL();
                openadmin.openadmin();
            driver1.navigate().to("https://www.cateringforcorporate.com/admin/manage-restaurant");
           
            }

On Thu, May 17, 2018 at 7:12 PM, Krishnan Mahadevan <[hidden email]> wrote:

The problem is in your test code. You are not invoking the 3 arg variant of assertEquals() method, wherein you pass in a string as well. Here’s a sample that shows this in action and the output as well.

 

import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

public class NewTest {
 
private LoggingAssert m_assert = new LoggingAssert();

 
@Test
 
public void
justCheck() {
   
m_assert.assertEquals("hello", "hello", "Ensuring that both hellos are equal");
  }

 
@AfterClass
   
public void afterClass() {
     
m_assert.getMessages().forEach(System.err::println);
  }
}

 

Ensuring that both hellos are equal

PASSED

Test:Ensuring that both hellos are equal

Test:Ensuring that both hellos are equal

 

===============================================

Default Suite

Total tests run: 1, Failures: 0, Skips: 0

===============================================

 

 

Process finished with exit code 0

 

 

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/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

From: <[hidden email]> on behalf of Shreejit Nair <[hidden email]>
Reply-To: <[hidden email]>
Date: Thursday, May 17, 2018 at 5:25 PM
To: testng-users <[hidden email]>
Subject: [testng-users] IAssert.getMessage() is null

 

Hi,

 

As part of overriding the onAssertSuccess to get some custom log messages printed out I was not able to get the Iassert instance getMessage() that always seems to be throwing a null.

 

Logging class:

public class LoggingAssert extends Assertion {

 

       private List<String> m_messages = Lists.newArrayList();

       @Override

       public void onBeforeAssert(IAssert a) {

              m_messages.add("Test:" + a.getMessage());

       }

      

       @Override

       public void onAssertSuccess(IAssert a) {

              m_messages.add("Test:" + a.getMessage());

              System.out.println(a.getMessage());

              System.out.println("PASSED");

       }

       @Override

       public void onAssertFailure(IAssert a) {

              m_messages.add("Test:" + a.getMessage());

              System.out.println("FAILED");

       }

      

       public List<String> getMessages() {

              return m_messages;

       }

}

 

Simple TestNG class:

public class NewTest {

       private LoggingAssert m_assert = new LoggingAssert();

 

       @Test

       public void justCheck() {

              m_assert.assertEquals("hello", "hello");

       }

}

 

I see the following in console:

null. // System.out.println(a.getMessage()); seems to be null

PASSED

PASSED: justCheck

 

I was looking to leverage this functionality to integrate with the Log4j logging framework in place.

--
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
[hidden email].
To post to this group, send email to
[hidden email].
Visit this group at
https://groups.google.com/group/testng-users.
For more options, visit
https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.