Asserts: Assertions in our test suites are required to validate
the actual result with expected result. The Assert class provided by
TestNG provides a form of hard assertion wherein as soon as an assertion
fails the execution of that particular test method stops and the test
method is marked as failure.
Following are the different assertions available in TestNg:
- assertEquals
- assertNotEquals
- assertTrue
- assertFalse
- assertNull
- assertNotNull
AsserEquals is used to compare expected and actual
values. Whenever the expected and actual values are same, the assertion
passes with no exception. But, if the actual and expected values are
different then assert fails with an exception and the
test is marked as “failed”. The suite continues to run with the next
@Test annotation(if any).
Syntax:
Assert.assertEquals(actual,expected);
Example:
@Test
public void assertEquals_1_PassingMethod()
{
String string_1="https://mahantesh-hadimani.blogspot.com";
String string_2="https://mahantesh-hadimani.blogspot.com";
Assert.assertEquals(string_1,string_2);
System.out.println("After Assertion Statment");
}
In the above Test Method both String_1 and String_2 matches Hence this
method will get pass. Hence After assertion statement will get
print.
@Test
public void assertEquals_2_FailingMethod()
{
String string_1="https://mahantesh-hadimani.blogspot.com";
String string_2="https://www.google.com/";
Assert.assertEquals(string_1,string_2);
System.out.println("After Assertion Statment");
}
In the above Test Method String_1 and String_2 are
not matches Hence this method will get Fail. Hence After assertion
statement will not print.
2. AssertNotEquals:
assertNotEquals is just opposite to the functioning of assertEquals
assertion. Whenever the expected and actual values are same, then
assertion fails with an exception and marks the test-case as “failed”.
If actual and expected are not matches, then test will pass.
Syntax:
Assert.assertNotEquals(actual,expected,Message);
Example:
@Test
public void assertNotEquals_1_PassingMethod(){
String string_1="Automation Testing";
String string_2="Software Testing";
Assert.assertNotEquals(string_1,string_2);
System.out.println("After Assertion Statment");
}
In the above Example for AssertNotEquals assertion,
String_1 and String_2 are not matches each other.
Hence this method will get Pass. Hence After assertion statement will
print.
@Test
public void assertNotEquals_2_FailingMethod(){
String string_1="Automation Testing";
String string_2="Automation Testing";
Assert.assertNotEquals(string_1,string_2);
System.out.println("After Assertion Statment");
}
In the above Example for AssertNotEquals assertion,
String_1 and String_2 are matches each other. Hence
this method will get Fail. Hence After assertion statement will not
print.
TestNg Output:
When we are dealing with Boolean conditions, we should use assertTrue.
This assertion returns true if the applied condition passes. If the
condition is false, this assertion skips the current method from
execution.
Syntax:
Assert.assertTrue(condition);
Example:
@Test
public void assertTrue_1_PassingMethod(){
int a=10;
int b=20;
Assert.assertTrue(a<b);
System.out.println("After Assertion Statment");
}
In the above Example for AssertTrue assertion, a=10 is less than b =20
so assertion condition A<B satisfies. Hence this method will get
Pass. Hence After assertion statement will Print.
@Test
public void assertTrue_2_FailingMethod(){
int a=20;
int b=10;
Assert.assertTrue(a<b);
System.out.println("After Assertion Statment");
}
In the above Example for AssertTrue assertion, a=20 is not less than b
=10 so assertion condition A<B does not satisfies. Hence this method
will get Fail. Hence After assertion statement will not Print.
TestNg Output:
4. AssertFalse:
Assert.assertFalse checks the Boolean value returned by a condition is
false or not. When a condition value is true, the assertion aborts the
method by an exception. This is basically opposite to assertTrue.
Syntax:
Assert.assertFalse(condition);
Example:
@Test
public void assertFalse_1_PassingMethod(){
int a=20;
int b=50;
Assert.assertFalse(a==b);
System.out.println("After Assertion Statment");
}
In the above Example for AssertFalse assertion, a=20 is not equal to b
=50 so assertion condition A==B does not satisfies. Hence this method
will get pass. Hence After assertion statement will
Print.
@Test
public void assertFalse_2_FailingMethod(){
int a=20;
int b=20;
Assert.assertFalse(a==b);
System.out.println("After Assertion Statment");
}
In the above Example for AssertFalse assertion, a=20 is equal to b =20
so assertion condition A==B satisfies. Hence this
method will get Fail. Hence After assertion statement will not
Print.
Console Output:
TestNg Output:
5. AssertNull:
This assertion checks for a object, if it is null or not. When an
object is ‘null’ the assertion returns an exception resulting in
aborting the test.
Syntax:
Assert.assertNull(object);
Example:
@Test
public void assertNull_1_PassingMethod(){
String a=null;
Assert.assertNull(a);
System.out.println("After Assertion Statment");
}
In the above Example for AssertNull assertion, a=Null, so assertion
condition is A is Null satisfies. Hence this method will get pass. Hence
After assertion statement will Print.
@Test
public void assertNull_2_FailingMethod(){
String a= "Asserts";
Assert.assertNull(a);
System.out.println("After Assertion Statment");
}
In the above Example for AssertNull assertion, a="Asserts", so assertion condition is A is
Null does not satisfies. Hence this method will get Fail. Hence After
assertion statement will not Print.
6.
AssertNotNull:
Assert.assertNotNull is vice-versa of assertNull. When a object has
some value, the assertion aborts the method with an exception.
Syntax:
Assert.assertNotNull(object);
Example:
@Test
public void assertNotNull_1_PassingMethod(){
String a= "Bengalore";
Assert.assertNotNull(a);
System.out.println("After Assertion Statment");
}
In the above Example for AssertNotNull assertion, a=Bengalore, so
assertion condition is A is not Null satisfies. Hence this method will
get pass. Hence After assertion statement will
Print.
@Test
public void assertNotNull_2_FailingMethod(){
String a= null;
Assert.assertNotNull(a);
System.out.println("After Assertion Statment");
}
In the above Example for AssertNotNull assertion, a=Null, so assertion
condition is A is not Null does not satisfies. Hence this method will
get Fail. Hence After assertion statement will not Print.
TestNg Output:
Complete program with all the above assertions with examples:
package testCases;
import org.testng.Assert;
import org.testng.annotations.Test;
public class Assertions_1 {
@Test
public void assertEquals_1_PassingMethod(){
String string_1="https://mahantesh-hadimani.blogspot.com";
String string_2="https://mahantesh-hadimani.blogspot.com";
Assert.assertEquals(string_1,string_2);
System.out.println("After Assertion Statment");
}
@Test
public void assertEquals_2_FailingMethod(){
String string_1="https://mahantesh-hadimani.blogspot.com";
String string_2="https://www.google.com/";
Assert.assertEquals(string_1,string_2);
System.out.println("After Assertion Statment");
}
@Test
public void assertNotEquals_1_PassingMethod(){
String string_1="Automation Testing";
String string_2="Software Testing";
Assert.assertNotEquals(string_1,string_2);
System.out.println("After Assertion Statment");
}
@Test
public void assertNotEquals_2_FailingMethod(){
String string_1="Automation Testing";
String string_2="Automation Testing";
Assert.assertNotEquals(string_1,string_2);
System.out.println("After Assertion Statment");
}
@Test
public void assertTrue_1_PassingMethod(){
int a=10;
int b=20;
Assert.assertTrue(a<b);
System.out.println("After Assertion Statment");
}
@Test
public void assertTrue_2_FailingMethod(){
int a=20;
int b=10;
Assert.assertTrue(a<b);
System.out.println("After Assertion Statment");
}
@Test
public void assertFalse_1_PassingMethod(){
int a=20;
int b=50;
Assert.assertFalse(a==b);
System.out.println("After Assertion Statment");
}
@Test
public void assertFalse_2_FailingMethod(){
int a=20;
int b=20;
Assert.assertFalse(a==b);
System.out.println("After Assertion Statment");
}
@Test
public void assertNull_1_PassingMethod(){
String a=null;
Assert.assertNull(a);
System.out.println("After Assertion Statment");
}
@Test
public void assertNull_2_FailingMethod(){
String a= "Asserts";
Assert.assertNull(a);
System.out.println("After Assertion Statment");
}
@Test
public void assertNotNull_1_PassingMethod(){
String a= "Bengalore";
Assert.assertNotNull(a);
System.out.println("After Assertion Statment");
}
@Test
public void assertNotNull_2_FailingMethod(){
String a= null;
Assert.assertNotNull(a);
System.out.println("After Assertion Statment");
}
}
Console Output:
[TestNG] Running:
C:\Users\mahantesh.hadimani\AppData\Local\Temp\testng-eclipse--623077862\testng-customsuite.xml
After Assertion Statment
After Assertion Statment
After Assertion Statment
After Assertion Statment
After Assertion Statment
After Assertion Statment
PASSED: assertEquals_1_PassingMethod
PASSED: assertFalse_1_PassingMethod
PASSED: assertNotEquals_1_PassingMethod
PASSED: assertNotNull_1_PassingMethod
PASSED: assertNull_1_PassingMethod
PASSED: assertTrue_1_PassingMethod
FAILED: assertEquals_2_FailingMethod
java.lang.AssertionError: expected [https://www.google.com/] but found [https://mahantesh-hadimani.blogspot.com]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertEqualsImpl(Assert.java:135)
at org.testng.Assert.assertEquals(Assert.java:116)
at org.testng.Assert.assertEquals(Assert.java:190)
at org.testng.Assert.assertEquals(Assert.java:200)
at testCases.Assertions_1.assertEquals_2_FailingMethod(Assertions_1.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
FAILED: assertFalse_2_FailingMethod
java.lang.AssertionError: expected [false] but found [true]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertFalse(Assert.java:63)
at org.testng.Assert.assertFalse(Assert.java:73)
at testCases.Assertions_1.assertFalse_2_FailingMethod(Assertions_1.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
FAILED: assertNotEquals_2_FailingMethod
java.lang.AssertionError: null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.assertNotEquals(Assert.java:831)
at org.testng.Assert.assertNotEquals(Assert.java:836)
at testCases.Assertions_1.assertNotEquals_2_FailingMethod(Assertions_1.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
FAILED: assertNotNull_2_FailingMethod
java.lang.AssertionError: expected object to not be null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.assertNotNull(Assert.java:423)
at org.testng.Assert.assertNotNull(Assert.java:408)
at testCases.Assertions_1.assertNotNull_2_FailingMethod(Assertions_1.java:115)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
FAILED: assertNull_2_FailingMethod
java.lang.AssertionError: expected [null] but found [Asserts]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotSame(Assert.java:509)
at org.testng.Assert.assertNull(Assert.java:445)
at org.testng.Assert.assertNull(Assert.java:434)
at testCases.Assertions_1.assertNull_2_FailingMethod(Assertions_1.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
FAILED: assertTrue_2_FailingMethod
java.lang.AssertionError: expected [true] but found [false]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertTrue(Assert.java:42)
at org.testng.Assert.assertTrue(Assert.java:52)
at testCases.Assertions_1.assertTrue_2_FailingMethod(Assertions_1.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
===============================================
Default test
Tests run: 12, Failures: 6, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 12, Failures: 6, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@12bb4df8: 49 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@61064425: 23 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@16f65612: 217 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@368239c8: 165 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@2437c6dc: 40 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 33 ms
TestNg Output:
No comments:
Post a Comment
If any suggestions or issue, please provide