1

I want to validate the presence of a text using assertTrue(), but I am getting a StackOverflow error. I am not sure that the code I have written is correct or not. Need your suggestions.

// Checking the posted text
WebElement postedtext= driver.findElement(By.cssSelector("css123"));
assertTrue("The text is verfied",postedtext.getText().equals(enteredText));

private static void assertTrue(String string, boolean equals) {
assertTrue(string,equals);}
1
  • Are you using TestNG or Junit? Logically I don't see any issue in this code block. OP have declared and invoked the same assertTrue(String string, boolean equals). No reason for atleast TestNG to interfere. Commented Jan 5, 2018 at 5:12

2 Answers 2

3

You have a method called assertTrue(s, b) which calls itself. This is causing an infinite recursion.

Sign up to request clarification or add additional context in comments.

2 Comments

What can I do here?
Delete your implementation of assertTrue() and simply call the one provided by your testing library (JUnit or whatever).
2

It is a name conflicting that you happened to name you assert method the same name as the library method. Rename your assertTrue can solve the problem.

// Checking the posted text
WebElement postedtext= driver.findElement(By.cssSelector("css123"));
myAssertTrue("The text is verfied",postedtext.getText().equals(enteredText));

private static void myAssertTrue(String string, boolean equals) {
    try {
        assertTrue(string,equals);
    } catch (AssertionError e) {
        System.out.println(e.getMessage());
        throw e;
    }
}

Or you just delete your assertTrue and use the library method instead.

2 Comments

It did the job. But I am not getting the string message in console. Is the string message suppose to come in console or we have to do something else here ?
@Alapan Das You have to catch AssertionError and get message from the error, I have updated my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.