Hard Assert



Hard Assert



Assertion is used to compare actual value with expected value, if this comparison fails, program will through AssertException. To handle this exception we can use catch block to catch the exception.

Below are the types of Assertions.

o AssertEquals

o AssertNotEquals

o AssertTrue

o AssertFalse

o AssertNull

o AssertNotNull



AssertEquals()

This method opposite of AssertNotEquals()In
this method, it will compare actual and expected result. If both actual and
expected are matching then the assert condition will pass and if it is not
matching then the assert condition will fail

Example- Below condition is true so assert will be passed.

 String actual="My name is Yogen";
String expected="My name is Yogen";
@Test
public void Testassertion()
{
Assert.assertEquals(actual, expected, "Atual is:-"+actual+"\n"+"Expected is:-"+expected+"\n"+"It is not Matching");
}

AssertNotEquals()

This method is used to compare actual and expected result, if both actual and expected are not matching then the condition will be pass and test script also pass. But if it is matching then the test script will failed.


Example- In below condition assert will fails as both strings are different.

String actual="My name is Yogen";
String expected="My name is Yogen";
@Test
public void Testassertion()
{
Assert.assertNotEquals(actual, expected, "Atual is:-"+actual+"\n"+"Expected is:-"+expected+"\n"+"It is not Matching");
}

AssertTrue()

This method opposite of AssertFalse(). This verify the Boolean value base on the condition given in the assert. If Boolean value is true then assert condition will be pass and if it is false then the assert condition will be fails.

Example- Below condition is true so assert will pass.

String actual="My name is Yogen";

String expected="My name is Yogen";
@Test
public void Testassertion()
{
Assert.assertTrue(actual.contains(expected), "Atual is:-"+actual+"\n"+"Expected is:-"+expected+"\n"+"It is not Matching");
}

AssertFalse()


This verify the Boolean value base on the condition given in the assert. If Boolean value is false then test script will be pass and if it is true then the test script will be fails.


Example- In below condition assert fail as both strings are same.

String actual="My name is Yogen";
String expected="My name is Yogen";
@Test
public void Testassertion()
{
Assert.assertFalse(actual.contains(expected), "Atual is:-"+actual+"\n"+"Expected is:-"+expected+"\n"+"It is not Matching");
}


AssertNull()


This method is used to check whether the object is null or not. So if object is null then assertion will be pass and if object is not null then assertion will fail.


Example- Below string “actual” has some value in it so assert will fail.

String actual="My name is Yogen";
String expected="My name is Yogen";
@Test
public void Testassertion()
{
Assert.assertNull(actual, "This is not null value");
}

AssertNotNull()

This method opposite of AssertNull()This method is used to check whether the object is null or not null. So if object is not null then assert condition will be pass and if object is null then assert condition will fail.

Example- Below string “actual” has some value in it so assert will true.

String actual="My name is Yogen";
String expected="My name is Yogen";
@Test
public void Testassertion()
{
Assert.assertNotNull(actual, "This has some value");
}







No comments:

Post a Comment

Software Testing Automation Guide

  get methods in Selenium Webdriver Below are the list of get methods that can be use with driver. Get()- This com...