Source:How to continue execution when Assertion is failed
httpClient.setReuseStrategy(new NoConnectionReuseStrategy());
Best Testng code snippet using org.testng.asserts.SoftAssert
Source:SoftAssertTest.java
1package test.assertion;2import org.testng.Assert;3import org.testng.annotations.Test;4import org.testng.asserts.IAssert;5import org.testng.asserts.SoftAssert;6import java.util.ArrayList;7import java.util.Collection;8public class SoftAssertTest {9 @Test10 public void testOnSucceedAndFailureCalled() throws Exception {11 final Collection<IAssert> succeed = new ArrayList<>();12 final Collection<IAssert> failures = new ArrayList<>();13 final SoftAssert sa = new SoftAssert() {14 @Override15 public void onAssertSuccess(IAssert assertCommand) {16 succeed.add(assertCommand);17 }18 @Override19 public void onAssertFailure(IAssert assertCommand, AssertionError ex) {20 failures.add(assertCommand);21 }22 };23 sa.assertTrue(true);24 sa.assertTrue(false);25 Assert.assertEquals(succeed.size(), 1, succeed.toString());26 Assert.assertEquals(failures.size(), 1, failures.toString());27 }28 @Test29 public void testAssertAllCount() throws Exception {30 String message = "My message";31 SoftAssert sa = new SoftAssert();32 sa.assertTrue(true);33 sa.assertTrue(false, message);34 try {35 sa.assertAll();36 Assert.fail("Exception expected");37 } catch (AssertionError e) {38 String[] lines = e.getMessage().split("\r?\n");39 Assert.assertEquals(lines.length, 2);40 lines[1] = lines[1].replaceFirst(message, "");41 Assert.assertFalse(lines[1].contains(message));42 }43 }44}...
Source:LoginTest.java
...4import org.testng.Assert;5import org.testng.annotations.Listeners;6import org.testng.annotations.Test;7import org.testng.asserts.Assertion;8import org.testng.asserts.SoftAssert;9import pom.pages.CandyPage;10import pom.pages.HomePage;111213public class LoginTest extends BaseTest{1415 private SoftAssert softAssert = new SoftAssert();16 private Assertion hardAssert = new Assertion();1718 @Test19 public void logIn(){20 loginPage.userLogin();21 softAssert.assertTrue(homePage.myPoints().isDisplayed(), "Result is not as expected");22 hardAssert.assertTrue(homePage.myPoints().getText().contains("MIS PUNTOS 3884"), "Result is not as expected");23 }2425 @Test26 public void logOut(){27 logIn();28 homePage.userIcon();29 homePage.logOut();
...
Source:TestOrangeHRM2.java
...20 @Test(priority = 1)21 public void verifyTheLoginPageTitle()22 {23 Reporter.log("verify the login page title",true);24 org.testng.asserts.SoftAssert sa1 = new org.testng.asserts.SoftAssert();25 sa1.assertEquals(true, true);26 sa1.assertAll();27 }28 29 @Test(priority = 2)30 public void validloginTest()31 {32 Reporter.log("Login To the application",true);33 Assert.assertEquals(false, true);34 35 }36 37 @Test(priority = 3)38 public void verifyHomePageTitle()39 {40 Reporter.log("verify Home Page Title",true);41 org.testng.asserts.SoftAssert sa2 = new org.testng.asserts.SoftAssert();42 sa2.assertEquals(true, true);43 sa2.assertAll();44 }45 46 @Test(priority = 4)47 public void addEmployee()48 {49 50 Reporter.log("Add the employee",true);51 }52}
Source:SoftAssertion.java
1package TestNGAssertions;23import org.testng.annotations.Test;4import org.testng.asserts.SoftAssert;56/*assertion is stored in org.testng.asserts.Softassert and we need to use this type of assertion7 * when we want to continue our execution even after the assertion got failed. 8 * Soft assertions will not be throwing an error when the assertions got failed, 9 * but it continues the execution to the next step*/1011public class SoftAssertion12{13 SoftAssert softAssert = new SoftAssert();14 String className = "SoftAssertion";1516 @Test17 public void test_UsingSoftAssertion() 18 {19 softAssert.assertTrue(true == true);20 softAssert.assertEquals("SoftAssert", "SoftAssertion");21 softAssert.assertEquals(className, "SoftAssertion");22 System.out.println("Last statement gets executed!");23 softAssert.assertAll();24 }
...
Source:testing_softassertion.java
1package test.softassertions;23import org.testng.annotations.Test;4import org.testng.asserts.Assertion;5import org.testng.asserts.SoftAssert;67public class testing_softassertion {89 private Assertion hardAssert = new Assertion();10 private SoftAssert softAssert = new SoftAssert();11 1213@Test14public void testHardAndSoftAssert() {15 softAssert.assertEquals(1, 1, "integer incorrect asserssion");16 softAssert. assertTrue(Boolean.TRUE, "boolean1 incorrect asserssion");17 softAssert.assertTrue(Boolean.FALSE, "boolean2 incorrect asserssion");18 softAssert.assertTrue(Boolean.TRUE, "boolean3 incorrect asserssion");19 softAssert.assertTrue(Boolean.FALSE, "boolean4 incorrect asserssion");20 softAssert.assertAll();21}22
...
Source:SoftAssert.java
1package Assertion;2import org.testng.Reporter;3import org.testng.annotations.Test;4public class SoftAssert {5 @Test6 public void b() 7 {8 Reporter.log("Launch the browser",true);9 Reporter.log("launch the web application",true);10 Reporter.log("Verify the login page title",true);11 org.testng.asserts.SoftAssert softassert = new org.testng.asserts.SoftAssert();12 softassert.assertEquals(false, true);13 Reporter.log("login to the application",true);14 15 Reporter.log("verify the home page title",true);16 17 softassert.assertAll();18 }19}...
Source:Assertions.java
1package com.lazywork.testframework.utils;2import org.testng.asserts.Assertion;3import org.testng.asserts.SoftAssert;4public class Assertions {5 public static void hardAssertionTrue(Boolean condition) {6 Assertion hardAssert = new Assertion();7 hardAssert.assertTrue(condition);8 }9 public static SoftAssert softAssertion(Boolean condition) {10 SoftAssert softAssert = new SoftAssert();11 softAssert.assertTrue(condition);12 return softAssert;13 // softAssert.assertAll();14 }15}...
Source:CustomSoftAssert.java
1package page.classes;2import org.openqa.selenium.WebDriver;3import org.testng.asserts.IAssert;4import org.testng.asserts.SoftAssert;5public class CustomSoftAssert extends SoftAssert {6 private static WebDriver driver = null;7 @Override8 public void onAssertFailure(IAssert<?> a, AssertionError ex){9 myUtilities.takeScreenshot(driver);10 }11}...
SoftAssert
Using AI Code Generation
1import org.testng.annotations.Test;2import org.testng.asserts.SoftAssert;3public class SoftAssertTest {4 public void testSoftAssert() {5 SoftAssert softAssert = new SoftAssert();6 System.out.println("Soft Assert Example");7 softAssert.assertEquals(12, 13, "Numbers are not equal");8 System.out.println("Line after first assert");9 softAssert.assertEquals("text", "text", "Strings are not equal");10 System.out.println("Line after second assert");11 softAssert.assertAll();12 }13}14Method testSoftAssert() has not completed successfully15at org.testng.internal.MethodInvocationHelper.wrapWithTimeout(MethodInvocationHelper.java:104)16at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:76)17at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)18at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)19at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1199)20at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)21at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)22at org.testng.TestRunner.privateRun(TestRunner.java:774)23at org.testng.TestRunner.run(TestRunner.java:624)24at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)25at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)26at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)27at org.testng.SuiteRunner.run(SuiteRunner.java:261)28at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)29at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)30at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)31at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)32at org.testng.TestNG.run(TestNG.java:1057)33at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)34at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
SoftAssert
Using AI Code Generation
1import org.testng.annotations.Test;2import org.testng.asserts.SoftAssert;3public class SoftAssertTest {4 public void softAssert() {5 SoftAssert softAssert = new SoftAssert();6 System.out.println("Soft Assert Example");7 softAssert.assertEquals(12, 13);8 System.out.println("Line after assert 1");9 softAssert.assertEquals(13, 13);10 System.out.println("Line after assert 2");11 softAssert.assertAll();12 }13}14at org.testng.asserts.SoftAssert.fail(SoftAssert.java:71)15at org.testng.asserts.SoftAssert.failNotEquals(SoftAssert.java:67)16at org.testng.asserts.SoftAssert.assertEquals(SoftAssert.java:37)17at org.testng.asserts.SoftAssert.assertEquals(SoftAssert.java:28)18at org.testng.asserts.SoftAssert.assertEquals(SoftAssert.java:23)19at com.automation.testng.SoftAssertTest.softAssert(SoftAssertTest.java:14)20at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)21at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)22at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)23at java.lang.reflect.Method.invoke(Method.java:498)24at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)25at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)26at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)27at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)28at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)29at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)30at org.testng.TestRunner.privateRun(TestRunner.java:756)31at org.testng.TestRunner.run(TestRunner.java:610)32at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)33at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)34at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)35at org.testng.SuiteRunner.run(SuiteRunner.java:289)36at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:
SoftAssert
Using AI Code Generation
1package testng;2import org.testng.Assert;3import org.testng.annotations.Test;4import org.testng.asserts.SoftAssert;5public class SoftAssertion {6 public void testSoftAssertion() {7 SoftAssert softAssert = new SoftAssert();8 System.out.println("SoftAssert Example");9 softAssert.assertEquals(12, 13);10 System.out.println("Line after first assert");11 softAssert.assertEquals("Hello", "Hello");12 System.out.println("Line after second assert");13 softAssert.assertAll();14 }15}16org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.testSoftAssertion() didn't finish within the time-out 30000017 at org.testng.internal.thread.ThreadTimeoutException.create(ThreadTimeoutException.java:51)18 at org.testng.internal.thread.ThreadTimeoutRunnable.run(ThreadTimeoutRunnable.java:40)19 at java.lang.Thread.run(Thread.java:748)20 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)21 at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)22 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)23 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)24 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)25 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)26 at org.testng.TestRunner.privateRun(TestRunner.java:774)27 at org.testng.TestRunner.run(TestRunner.java:624)28 at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)29 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)30 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)31 at org.testng.SuiteRunner.run(SuiteRunner.java:261)32 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)33 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)34 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)35 at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)36 at org.testng.TestNG.runSuites(TestNG.java:1069)37 at org.testng.TestNG.run(TestNG.java:1037)38 at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:115)
SoftAssert
Using AI Code Generation
1package com.packt.test;2import org.testng.annotations.Test;3import org.testng.asserts.SoftAssert;4public class SoftAssertTest {5public void softAssertTest() {6SoftAssert softAssert = new SoftAssert();7System.out.println("softAssertTest() started");8softAssert.assertEquals(1, 2);9System.out.println("softAssertTest() done");10softAssert.assertAll();11}12}13softAssertTest() started14softAssertTest() done15at org.testng.Assert.fail(Assert.java:94)16at org.testng.Assert.failNotEquals(Assert.java:494)17at org.testng.Assert.assertEquals(Assert.java:123)18at org.testng.Assert.assertEquals(Assert.java:370)19at org.testng.Assert.assertEquals(Assert.java:380)20at com.packt.test.SoftAssertTest.softAssertTest(SoftAssertTest.java:14)21at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)22at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)23at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)24at java.lang.reflect.Method.invoke(Method.java:498)25at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)26at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)27at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)28at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)29at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)30at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)31at org.testng.TestRunner.privateRun(TestRunner.java:648)32at org.testng.TestRunner.run(TestRunner.java:505)33at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)34at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)35at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
SoftAssert
Using AI Code Generation
1 SoftAssert softAssert = new SoftAssert();2 softAssert.assertEquals(1, 2);3 softAssert.assertEquals(2, 2);4 softAssert.assertAll();5 Assert.assertEquals(1, 2);6 Assert.assertEquals(2, 2);7 System.out.println("Test complete");8[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Test ---9[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Test ---10[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Test ---11[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Test ---12[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ Test ---
SoftAssert
Using AI Code Generation
1package com.test.automation;2import org.testng.Assert;3import org.testng.annotations.Test;4import org.testng.asserts.SoftAssert;5public class SoftAssertDemo {6 public void softAssert() {7 SoftAssert softAssert = new SoftAssert();8 System.out.println("softAssert Method Was Started");9 softAssert.assertEquals(12, 13);10 System.out.println("softAssert Method Was Executed");11 softAssert.assertAll();12 }13 public void hardAssert() {14 System.out.println("hardAssert Method Was Started");15 Assert.assertEquals(12, 13);16 System.out.println("hardAssert Method Was Executed");17 }18}19 at org.testng.Assert.fail(Assert.java:94)20 at org.testng.Assert.failNotEquals(Assert.java:494)21 at org.testng.Assert.assertEquals(Assert.java:123)22 at org.testng.Assert.assertEquals(Assert.java:370)23 at org.testng.Assert.assertEquals(Assert.java:380)24 at com.test.automation.SoftAssertDemo.hardAssert(SoftAssertDemo.java:20)25 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)26 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)27 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)28 at java.lang.reflect.Method.invoke(Method.java:498)29 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)30 at org.testng.internal.Invoker.invokeMethod(Invoker.java:599)31 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:174)32 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:146)33 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)34 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)35 at org.testng.TestRunner.privateRun(TestRunner.java:764)36 at org.testng.TestRunner.run(TestRunner.java:585)37 at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)38 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)39 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)40 at org.testng.SuiteRunner.run(SuiteRunner.java:286)
SoftAssert
Using AI Code Generation
1public class SoftAssert {2 public static void main(String[] args) {3 SoftAssert sa = new SoftAssert();4 sa.assertEquals("hello", "hello");5 sa.assertEquals("hello", "hello1");6 sa.assertEquals("hello", "hello2");7 sa.assertAll();8 }9}10package com.javabykiran;11import org.testng.annotations.Test;12import org.testng.asserts.SoftAssert;13public class SoftAssert {14 public void softAssert() {15 SoftAssert sa = new SoftAssert();16 sa.assertEquals("hello", "hello");17 sa.assertEquals("hello", "hello1");18 sa.assertEquals("hello", "hello2");19 sa.assertAll();20 }21}22package com.javabykiran;23import org.testng.Assert;24import org.testng.annotations.Test;25public class HardAssert {26 public void hardAssert() {27 Assert.assertEquals("hello", "hello");28 Assert.assertEquals("hello", "hello1");29 Assert.assertEquals("hello", "hello2");30 }31}32package com.javabykiran;33import org.testng.annotations.Test;34import org.testng.asserts.SoftAssert;35public class SoftAssert {36 public void softAssert() {37 SoftAssert sa = new SoftAssert();38 sa.assertEquals("hello", "hello");39 sa.assertEquals("hello", "hello1");40 sa.assertEquals("hello", "hello2");41 sa.assertAll();42 }43}44package com.javabykiran;45import org.testng.Assert;46import org.testng.annotations.Test;47public class HardAssert {48 public void hardAssert() {49 Assert.assertEquals("hello", "hello");50 Assert.assertEquals("hello", "hello1");51 Assert.assertEquals("hello", "hello2");52 }53}54package com.javabykiran;55import org.testng.annotations.Test;56import org.testng.asserts.SoftAssert;57public class SoftAssert {58 public void softAssert() {59 SoftAssert sa = new SoftAssert();60 sa.assertEquals("hello", "hello");61 sa.assertEquals("hello", "hello1");62 sa.assertEquals("hello", "hello2");63 sa.assertAll();64 }65}66package com.javabykiran;67import org.testng.Assert;68import org.testng.annotations.Test;69public class HardAssert {70 public void hardAssert() {
1httpClient.setReuseStrategy(new NoConnectionReuseStrategy());2
1public class RestTemplateCustomizers {2 static public class MaxConnectionTimeCustomizer implements RestTemplateCustomizer {34 @Override5 public void customize(RestTemplate restTemplate) {6 HttpClient httpClient = HttpClientBuilder7 .create()8 .setConnectionTimeToLive(1000, TimeUnit.MILLISECONDS)9 .build();1011 restTemplate.setRequestFactory(12 new HttpComponentsClientHttpRequestFactory(httpClient));13 }14 }15}1617// In your service that uses a RestTemplate18public MyRestService(RestTemplateBuilder builder ) {19 restTemplate = builder20 .customizers(new RestTemplateCustomizers.MaxConnectionTimeCustomizer())21 .build();22}23
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!