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

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

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

AbstractIterableAssert.extractingResultOf介绍

[英]Extract the result of given method invocation on the Iterable's elements under test into a new Iterable, this new Iterable becoming the Iterable under test.

It allows you to test the method results of the Iterable's elements instead of testing the elements themselves. This is especially useful for classes that do not conform to the Java Bean's getter specification (i.e. public String toString() or public String status() instead of public String getStatus()).

Let's take a look at an example to make things clearer :

// Build a array of WesterosHouse, a WesterosHouse has a method: public String sayTheWords() 
List<WesterosHouse> greatHouses = new ArrayList<WesterosHouse>(); 
greatHouses.add(new WesterosHouse("Stark", "Winter is Coming")); 
greatHouses.add(new WesterosHouse("Lannister", "Hear Me Roar!")); 
greatHouses.add(new WesterosHouse("Greyjoy", "We Do Not Sow")); 
greatHouses.add(new WesterosHouse("Baratheon", "Our is the Fury")); 
greatHouses.add(new WesterosHouse("Martell", "Unbowed, Unbent, Unbroken")); 
greatHouses.add(new WesterosHouse("Tyrell", "Growing Strong")); 
// let's verify the words of the great houses of Westeros: 
assertThat(greatHouses).extractingResultOf("sayTheWords") 
.contains("Winter is Coming", "We Do Not Sow", "Hear Me Roar") 
.doesNotContain("Lannisters always pay their debts");

Following requirements have to be met to extract method results:

  • method has to be public,
  • method cannot accept any arguments,
  • method cannot return void.

Note that the order of extracted results is consistent with the iteration order of the Iterable under test, for example if it's a HashSet, you won't be able to make any assumptions on the extracted results order.
[中]将给定方法调用被测Iterable元素的结果提取到一个新Iterable中,这个新Iterable将成为被测Iterable。
它允许您测试Iterable元素的方法结果,而不是测试元素本身。这对于不符合JavaBean的getter规范的类(即公共字符串toString()或公共字符串status()而不是公共字符串getStatus())特别有用。
让我们看一个例子让事情更清楚:

// Build a array of WesterosHouse, a WesterosHouse has a method: public String sayTheWords() 
List<WesterosHouse> greatHouses = new ArrayList<WesterosHouse>(); 
greatHouses.add(new WesterosHouse("Stark", "Winter is Coming")); 
greatHouses.add(new WesterosHouse("Lannister", "Hear Me Roar!")); 
greatHouses.add(new WesterosHouse("Greyjoy", "We Do Not Sow")); 
greatHouses.add(new WesterosHouse("Baratheon", "Our is the Fury")); 
greatHouses.add(new WesterosHouse("Martell", "Unbowed, Unbent, Unbroken")); 
greatHouses.add(new WesterosHouse("Tyrell", "Growing Strong")); 
// let's verify the words of the great houses of Westeros: 
assertThat(greatHouses).extractingResultOf("sayTheWords") 
.contains("Winter is Coming", "We Do Not Sow", "Hear Me Roar") 
.doesNotContain("Lannisters always pay their debts");

提取方法结果必须满足以下要求:
*方法必须是公开的,
*方法无法接受任何参数,
*方法不能返回void。
请注意,提取结果的顺序与被测Iterable的迭代顺序一致,例如,如果它是哈希集,则无法对提取结果的顺序进行任何假设。

代码示例

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

assertThat(hosts).hasSize(2).extractingResultOf("isUp").containsOnly(true);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

assertThat(cluster.getMetadata().getAllHosts())
  .hasSize(3)
  .extractingResultOf("getAddress")
  .contains(node2Address);
assertThat(cluster.getMetadata().getAllHosts())
  .hasSize(2)
  .extractingResultOf("getAddress")
  .doesNotContain(node2Address);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

assertThat(e.getErrors().values())
  .hasOnlyElementsOfType(OperationTimedOutException.class)
  .extractingResultOf("getMessage")
  .containsOnlyOnce(
    String.format(

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

assertThat(e.getErrors().values())
  .hasOnlyElementsOfType(OperationTimedOutException.class)
  .extractingResultOf("getMessage")
  .containsOnlyOnce(
    String.format(

代码示例来源:origin: com.datastax.dse/dse-java-driver-core

assertThat(hosts).hasSize(2).extractingResultOf("isUp").containsOnly(true);

代码示例来源:origin: com.datastax.dse/dse-java-driver-core

assertThat(cluster.getMetadata().getAllHosts())
  .hasSize(3)
  .extractingResultOf("getAddress")
  .contains(node2Address);
assertThat(cluster.getMetadata().getAllHosts())
  .hasSize(2)
  .extractingResultOf("getAddress")
  .doesNotContain(node2Address);

代码示例来源:origin: com.datastax.dse/dse-java-driver-core

assertThat(e.getErrors().values())
  .hasOnlyElementsOfType(OperationTimedOutException.class)
  .extractingResultOf("getMessage")
  .containsOnlyOnce(
    String.format(

代码示例来源:origin: com.datastax.dse/dse-java-driver-core

assertThat(e.getErrors().values())
  .hasOnlyElementsOfType(OperationTimedOutException.class)
  .extractingResultOf("getMessage")
  .containsOnlyOnce(
    String.format(

相关文章

微信公众号

最新文章

更多