Wednesday, 1 February 2017

Soft Assertion util. VerifyTestUtil

As part of the project, we might need to soft assert our assertions and show all error message at a time instead of breaking the execution then and their.

here is the solution.


import java.util.ArrayList;

import org.testng.Assert;

/**
 * Class to verify multiple assertions and sums of error messages
 *
 */
public class VerifyTestUtil {
private ArrayList<String> results = new ArrayList<String>();

/**
* method to verify one condition and add err message ot err list
*
* @param condition
* @param message
*/
public void verify(boolean condition, String message) {
if (!condition)
results.add(message);
}

/**
* final verification to check whether the test is passed or not
*
* @return
*/
public boolean isPassed() {
return results.size() == 0;
}

/**
* Method to read messages
*
* @return
*/
public String getMessage() {
return results.toString();
}

/**
* final method to execute
*
* @return
*/
public boolean assertTestResult() {
for (String str : results)
System.out.println(str);
Assert.assertTrue(results.size() == 0, results.toString());
return results.size() == 0;
}

/**
* verifies result
*
* @param message
* @return
*/
public boolean assertTestResult(String message) {
for (String str : results)
System.out.println(str);
Assert.assertTrue(results.size() == 0, message + " " + results.toString());
return results.size() == 0;
}

/**
* compare two strings and append err message if any
*
* @param one
* @param two
* @param message
* @return
*/
public boolean assertEqual(String one, String two, String message) {
if (one.contentEquals(two))
return true;
else {
results.add(message);
return false;
}
}

/**
* comares two integers
*
* @param one
* @param two
* @param message
* @return
*/
public boolean assertEqual(int one, int two, String message) {
if (one == two)
return true;
else {
results.add(message);
return false;
}
}

/**
* method to asserts  true
* @param condition
* @param message
*/
public void assertTrue(boolean condition, String message) {
Assert.assertTrue(condition, message);
}
}

No comments:

Post a Comment