Skip to content

Instantly share code, notes, and snippets.

@eliasnogueira
Created March 16, 2020 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasnogueira/afb16b5a9f0b59d1d3535a161eed3ecf to your computer and use it in GitHub Desktop.
Save eliasnogueira/afb16b5a9f0b59d1d3535a161eed3ecf to your computer and use it in GitHub Desktop.
Example showing different ways to assert a text
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.apache.commons.lang3.StringUtils;
import org.testng.annotations.Test;
public class AssertionTest {
public static final String ACTUAL = "text wrong here";
public static final String EXPECTED = "text right here";
// not recommended
@Test
public void usingAssertTrue() {
assertTrue(ACTUAL.equals(EXPECTED));
}
// best one
@Test
public void usingAssertEqual() {
assertEquals(ACTUAL, EXPECTED);
}
// not recommended
@Test
public void usingAssertTrueAndStringUtils() {
assertTrue(StringUtils.equals(ACTUAL, EXPECTED));
}
}
@eliasnogueira
Copy link
Author

eliasnogueira commented Mar 16, 2020

Results

usingAssertTrue

The problem here is: the error is not clear as the differences are true or false

java.lang.AssertionError: expected [true] but found [false]
Expected :true
Actual   :false

usingAssertEqual

The good point here is: the error is clear and shows the text differences

java.lang.AssertionError: expected [text right here] but found [text wrong here]
Expected :text right here
Actual   :text wrong here

usingAssertTrueAndStringUtils

The problem here is: the error is not clear as the differences are true or false

java.lang.AssertionError: expected [true] but found [false]
Expected :true
Actual   :false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment