2015-04-28 64 views
1

这里的a link任何方式TestNG的软断言显示的assertEquals错误信息与给定的自定义消息一起

有没有办法用软断言给出的自定义消息一起显示默认的assertEquals错误消息?

我的要求是有自定义消息和断言错误消息如下。 “brokedown预期[1],但发现[0]”

import org.testng.annotations.Test; 
import org.testng.asserts.SoftAssert; 

public class SoftAsert 
{ 
    @Test 
    public void test() 
    { 
     SoftAssert asert=new SoftAssert(); 
     asert.assertEquals(false, true,"failed"); 
     asert.assertEquals(0, 1,"brokedown"); 
     asert.assertAll(); 
    } 
} 

回答

0

您可以创建自己的SoftAssert,这应该做的魔力:

public class MySoftAssert extends Assertion 
{ 
    // LinkedHashMap to preserve the order 
    private Map<AssertionError, IAssert> m_errors = Maps.newLinkedHashMap(); 

    @Override 
    public void executeAssert(IAssert a) { 
     try { 
      a.doAssert(); 
     } catch(AssertionError ex) { 
      onAssertFailure(a, ex); 
      m_errors.put(ex, a); 
     } 
    } 

    public void assertAll() { 
     if (! m_errors.isEmpty()) { 
      StringBuilder sb = new StringBuilder("The following asserts failed:\n"); 
      boolean first = true; 
      for (Map.Entry<AssertionError, IAssert> ae : m_errors.entrySet()) { 
       if (first) { 
        first = false; 
       } else { 
        sb.append(", "); 
       } 
       sb.append(ae.getKey().getMessage()); 
      } 
      throw new AssertionError(sb.toString()); 
     } 
    } 
} 
+0

呀,好像这只是现在的解决方案。 –

相关问题