简体   繁体   中英

@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();

      }

    }

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.

One of the assertions in softassert() , so when you use sa.assertAll(); at the end of the method an exception is being thrown. From the docs

When an assertion fails, don't throw an exception but record the failure. Calling assertAll() will cause an exception to be thrown if at least one assertion failed.

If you want to fail the test if any assertion has failed call assertAll(); at the end of the test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM