'Display errors in JUnit failure trace

I use Selenium Web Driver in Eclipse with JUnit. I need create simple checking if some text exists on the page - if it is, than I need generate error. I want this error to be displayed in JUnit failure trace. This is my code:

public class Check  {
@Rule
public ErrorCollector errCollector = new ErrorCollector();

@FindBy(tagName="body")
@CacheLookup
private WebElement titl;
@FindBy(partialLinkText="Exception")
@CacheLookup
private WebElement excep;


public void check_false(String t, String r) {
        try {
             String bodyText = titl.getText();
             Assert.assertFalse("Exception found", bodyText.contains(t)); }
         catch (AssertionError e) {
             System.err.println(r + ": " + e.getMessage());
             System.out.println(excep.getText());
             errCollector.addError(e);   
            }    
          }

If I get error, it is displayed in Eclipse consol, but test if shown as without error in JUnit and no exception message is displayed. How can I make checking properly?



Solution 1:[1]

I use

AssertJUnit.assertFalse("Exception found", bodyText.contains(t));

It's from http://testng.org/doc/index.html see http://testng.org/javadoc/org/testng/AssertJUnit.html

Within eclipse when my test fails, in the junit window I get the stackstrace. Have never tried collecting the errors. It would throw an

AssertionFailedError

if the test fails but if you catch it, I don't know if the stacktrace will be in the JUnit window.

Solution 2:[2]

This might not be quite what you're after, in which case ignore it, but you can simply fail it and supply an optional message as to why.

public void checkText(String actual, String expected){
  try{
    if(!expected.equals(actuals){
      fail("Expected : [ " expected + " ] , actual [" + actual + "]"
  }catch(SomeOtherException soe){
      fail(soe.getMessage());
  }
 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2