0

I'm using TestNG to run a test and I've been using the Assert.assert statements in Java.

Now there are a few places where I would like to validate whether a function returned true or not. If it didn't then I don't want to fail the test but would rather have it log a message and continue.

In effect, I'm trying something like this:

boolean return1 = funct1();
if(bool){
System.out.println("funct1 returned a false. An error occurred which needs to be investigated later but the test will continue for now")
}

I could of course do this via the code that I wrote but is there a more elegant method of doing this via TestNG?

How can I make some assertions without failing the test? SoftAssert seems to fail the test at the end when I call assertAll().

  • The solution you write seems explicit and elegant. What makes you think it is not elegant? – dzieciou May 21 at 4:00
  • @dzieciou he have to write extra Boolean function for each type of validations – PDHide May 21 at 4:56
  • @dzieciou if he want to check greater than , then he have to write a function that checks greater than , if he wants equal then he need a function , for everything he has to reinvent the wheel – PDHide May 21 at 4:57
  • @PHide. Not really. Many functions for checking conditions are already in Java. if (result > 5) {log.warning("Result to high %s", result );}. I guess what you mean is good diagnostics messages like "Expected result to be great than 5, actual value was 4", then I agree -- JUnit/TestNG assertions have that reporting builting in. – dzieciou May 21 at 5:07
2

You can catch the assert True,

if you read the documentation of asserttrue:

https://www.javadoc.io/doc/org.testng/testng/6.8.17/org/testng/Assert.html#assertEquals(boolean,%20boolean,%20java.lang.String)

you can see that asserttrue throws an error and not an exception. So you have to capture the Throwable.

ANd you can print it to console using System.err or use logger as Joao mentioned

    try {

        assertTrue(false," Test tfailed");

    } catch (Throwable e) {

        System.err.println(e);
   //     Logger logger = Logger.getLogger("");
   //   logger.log(Level.WARNING, e.toString());

    }

You can also use softAssert

        SoftAssert a= new SoftAssert();

        a.assertTrue(1==2, "safasfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
        a.assertTrue(1==2, "sad");
        a.assertTrue(1==2, "test");
        a.assertTrue(1==2, "3");

        try {

            a.assertAll();

        } catch (Throwable  e) {

            System.err.println(e);
        //  Logger logger = Logger.getLogger("");
       //   logger.log(Level.WARNING, e.toString());

        }

Output:

enter image description here

| improve this answer | |
  • You can use system.err.println instead of logger – PDHide May 26 at 6:11
2

Java has a logging feature built-in.

You would probably log a Severe or Warning message.

Example:

Logger logger = Logger.getLogger(MyClass.class.getName());
logger.log(Level.SEVERE, "My message");

Naturally, you may want to wrap this logging alongside assertions.

class Verifier {
  ...
  public void verifyEquals(Object obj1, Object obj2) {
      try{
        assert obj1.equals(obj2);
      } catch (AssertionError e) {
        logger.log(obj1 + " is not equal to " + obj2);
      }
  } 
}
| improve this answer | |
  • 1
    +1 logger is a good suggestion it makes the error more visible than printing it – PDHide May 20 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.