org.assertj.core.api.Assertions.useRepresentation()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(120)

本文整理了Java中org.assertj.core.api.Assertions.useRepresentation()方法的一些代码示例,展示了Assertions.useRepresentation()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assertions.useRepresentation()方法的具体详情如下:
包路径:org.assertj.core.api.Assertions
类名称:Assertions
方法名:useRepresentation

Assertions.useRepresentation介绍

[英]Register a Representation that will be used in all following assertions.

Representation are used to format types in assertions error messages.

An alternative way of using a different representation is to register one as a service, this approach is described in Representation, it requires more work than this method but has the advantage of not having to do anything in your tests and it would be applied to all the tests globally

Example :

private class Example {} 
private class CustomRepresentation extends StandardRepresentation { 
// override needed to hook specific formatting 
 @Override 
public String toStringOf(Object o) { 
if (o instanceof Example) return "Example"; 
// fallback to default formatting. 
return super.toStringOf(o); 
} 
// change String representation 
 @Override 
protected String toStringOf(String s) { 
return "$" + s + "$"; 
} 
} 
Assertions.useRepresentation(new CustomRepresentation()); 
// this assertion fails ... 
assertThat(new Example()).isNull(); 
// ... with error : 
// "expected:<[null]> but was:<[Example]>" 
// this one fails ... 
assertThat("foo").startsWith("bar"); 
// ... with error : 
// Expecting: 
//   <$foo$> 
// to start with: 
//   <$bar$>

[中]注册将在以下所有断言中使用的表示。
表示用于格式化断言错误消息中的类型。
使用不同表示法的另一种方法是将一个表示法注册为服务,这种方法在表示法中有描述,它需要比这种方法更多的工作,但其优点是不必在测试中执行任何操作,并且它将应用于全球所有测试
例子:

private class Example {} 
private class CustomRepresentation extends StandardRepresentation { 
// override needed to hook specific formatting 
 @Override 
public String toStringOf(Object o) { 
if (o instanceof Example) return "Example"; 
// fallback to default formatting. 
return super.toStringOf(o); 
} 
// change String representation 
 @Override 
protected String toStringOf(String s) { 
return "$" + s + "$"; 
} 
} 
Assertions.useRepresentation(new CustomRepresentation()); 
// this assertion fails ... 
assertThat(new Example()).isNull(); 
// ... with error : 
// "expected:<[null]> but was:<[Example]>" 
// this one fails ... 
assertThat("foo").startsWith("bar"); 
// ... with error : 
// Expecting: 
//   <$foo$> 
// to start with: 
//   <$bar$>

代码示例

代码示例来源:origin: org.assertj/assertj-core

Assertions.useRepresentation(customRepresentation);

代码示例来源:origin: joel-costigliola/assertj-core

Assertions.useRepresentation(customRepresentation);

代码示例来源:origin: facebook/litho

@Override
 public void evaluate() throws Throwable {
  final Activity activity = Robolectric.buildActivity(Activity.class).create().get();
  mContext = new ComponentContext(activity);
  setComponentStyleableAttributes();
  Assertions.useRepresentation(new LithoRepresentation(mContext));
  try {
   base.evaluate();
  } finally {
   Assertions.useDefaultRepresentation();
  }
 }
};

代码示例来源:origin: facebook/litho

final ComponentContext c = new ComponentContext(RuntimeEnvironment.application);
Assertions.useRepresentation(new LithoRepresentation(c));

相关文章