Search code examples
javaseleniumtestngassertions

@Test failed when calling method has softassert fails


Soft assert failed to continue testcase from parent class:

In below testing, the second method AssertTesting.softassert() is completed with failed soft assertion, but the run is stopped after the second method. I want to execute the third method AssertTesting.softassert2() from the parent class. Any other way I can use soft assert, so I can continue the execution

            package Assertion;

        import org.testng.annotations.Test;

        public class AssertTestingAll 
        {
            @Test
            public void callallAssert()
            {
                AssertTesting.Hardassert();
                AssertTesting.softassert();
                AssertTesting.softassert2();
            }

        }



    package Assertion;

    import org.junit.Assert;
    import org.testng.annotations.Test;
    import org.testng.asserts.SoftAssert;

    public class AssertTesting 
    {
        static SoftAssert sa=new SoftAssert();


      public static void Hardassert() 
      {

    //    Assert.fail("Assertion fail");

          Assert.assertEquals("strings dont match:", "Test", "Test");

          {
          String [] name1= {"jeeva","balan","madhu"};

          String [] name2= {"jeeva","balan","madhu"};

          Assert.assertArrayEquals("name does not match", name1, name2);
          }


          Assert.assertFalse("numbers does not matches ", 2<2);

          Assert.assertSame("jeeva", "jeeva");



      }

      public static void softassert()
      {
          sa.assertEquals("strings dont match:", "Test1", "Test");

          {
          String [] name1= {"jeeva","balan","madhu"};

          String [] name2= {"jeeva","balan","madhu"};


          Assert.assertArrayEquals("name does not match", name1, name2);
          }
          System.out.println("execution continued");

          sa.assertFalse(2<2,"numbers does not matches ");

          sa.assertSame("jeeva", "jeeva1");
          sa.assertAll();

      }


      public static void softassert2()
      {
          sa.assertEquals("strings dont match:", "Test1", "Test");

          {
          String [] name1= {"jeeva","balan","madhu"};

          String [] name2= {"jeeva","balan","madhu"};


          Assert.assertArrayEquals("name does not match", name1, name2);
          }
          System.out.println("execution continued");

          sa.assertFalse(2<2,"numbers does not matches ");

          sa.assertSame("jeeva", "jeeva1");
          sa.assertAll();

      }

    }

Solution

  • Methods of SoftAssert like assertEquals or assertFalse only record if the condition is fine or not and they do not throw any exception that indicates test failure to TestNG runner. The actual check and exception throwing happens in assertAll method.

    This explains why you get this error. softassert method runs assertAll which throws exception and the test stops its execution at that point.

    If you want the test to continue you need to use the same SoftAssert instance for all tests and run assertAll at the very end of the whole test.