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

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

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

AbstractObjectArrayAssert.extracting介绍

[英]Extract the values of given field or property from the array's elements under test into a new list, this new list becoming the object under test.

It allows you to test a field/property of the array's elements instead of testing the elements themselves, which can be much less work !

Let's take an example to make things clearer :

// Build a array of TolkienCharacter, a TolkienCharacter has a name (String) and a Race (a class) 
// they can be public field or properties, both works when extracting their values. 
TolkienCharacter[] fellowshipOfTheRing = new TolkienCharacter[] { 
new TolkienCharacter("Frodo", 33, HOBBIT), 
new TolkienCharacter("Sam", 38, HOBBIT), 
new TolkienCharacter("Gandalf", 2020, MAIA), 
new TolkienCharacter("Legolas", 1000, ELF), 
new TolkienCharacter("Pippin", 28, HOBBIT), 
new TolkienCharacter("Gimli", 139, DWARF), 
new TolkienCharacter("Aragorn", 87, MAN, 
new TolkienCharacter("Boromir", 37, MAN) 
}; 
// let's verify the names of TolkienCharacter in fellowshipOfTheRing : 
assertThat(fellowshipOfTheRing).extracting("name") 
.contains("Boromir", "Gandalf", "Frodo") 
.doesNotContain("Sauron", "Elrond"); 
// you can also extract nested field/property like the name of Race : 
assertThat(fellowshipOfTheRing).extracting("race.name") 
.contains("Hobbit", "Elf") 
.doesNotContain("Orc");

A property with the given name is looked for first, if it does not exist then a field with the given name is looked for.

Note that the order of extracted field/property values is consistent with the array order.
[中]将给定字段或属性的值从被测数组的元素中提取到一个新列表中,这个新列表将成为被测对象。
它允许您测试数组元素的字段/属性,而不是测试元素本身,这可以大大减少工作量!
让我们举一个例子让事情变得更清楚:

// Build a array of TolkienCharacter, a TolkienCharacter has a name (String) and a Race (a class) 
// they can be public field or properties, both works when extracting their values. 
TolkienCharacter[] fellowshipOfTheRing = new TolkienCharacter[] { 
new TolkienCharacter("Frodo", 33, HOBBIT), 
new TolkienCharacter("Sam", 38, HOBBIT), 
new TolkienCharacter("Gandalf", 2020, MAIA), 
new TolkienCharacter("Legolas", 1000, ELF), 
new TolkienCharacter("Pippin", 28, HOBBIT), 
new TolkienCharacter("Gimli", 139, DWARF), 
new TolkienCharacter("Aragorn", 87, MAN, 
new TolkienCharacter("Boromir", 37, MAN) 
}; 
// let's verify the names of TolkienCharacter in fellowshipOfTheRing : 
assertThat(fellowshipOfTheRing).extracting("name") 
.contains("Boromir", "Gandalf", "Frodo") 
.doesNotContain("Sauron", "Elrond"); 
// you can also extract nested field/property like the name of Race : 
assertThat(fellowshipOfTheRing).extracting("race.name") 
.contains("Hobbit", "Elf") 
.doesNotContain("Orc");

首先查找具有给定名称的属性,如果该属性不存在,则查找具有给定名称的字段。
请注意,提取的字段/属性值的顺序与数组顺序一致。

代码示例

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

@Override
@SafeVarargs
public final AbstractListAssert<?, List<? extends Tuple>, Tuple, ObjectAssert<Tuple>> extracting(Function<ELEMENT, ?>... extractors) {
 return super.extracting(extractors);
}

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

@Override
@SafeVarargs
public final AbstractListAssert<?, List<? extends Tuple>, Tuple, ObjectAssert<Tuple>> extracting(Function<? super ELEMENT, ?>... extractors) {
 return super.extracting(extractors);
}

代码示例来源:origin: bonitasoft/bonita-engine

@Test
  public void should_build_current_folder() throws Exception {
    //given
    ConfigurationFolderUtil configurationFolderUtil = new ConfigurationFolderUtil();
    Path setupFolder = temporaryFolder.newFolder().toPath();

    //when
    configurationFolderUtil.buildCurrentFolder(setupFolder);

    //then
    final File expectedFolder = setupFolder.resolve(PLATFORM_CONF_FOLDER_NAME).resolve("current").resolve("platform_init_engine").toFile();
    assertThat(expectedFolder).exists().isDirectory();
    assertThat(expectedFolder.listFiles()).extracting("name").hasSize(1).containsOnly("currentConfig.properties");
  }
}

代码示例来源:origin: bonitasoft/bonita-engine

@Test
public void should_copy_all_sql_files() throws Exception {
  //given
  ConfigurationFolderUtil configurationFolderUtil = new ConfigurationFolderUtil();
  Path setupFolder = temporaryFolder.newFolder().toPath();
  //when
  configurationFolderUtil.buildSqlFolder(setupFolder, "h2");
  //then
  final File expectedFolder = setupFolder.resolve(PLATFORM_CONF_FOLDER_NAME).resolve("sql").resolve("h2").toFile();
  assertThat(expectedFolder).exists().isDirectory();
  assertThat(expectedFolder.listFiles()).extracting("name").hasSize(10).containsOnly(ALL_SQL_FILES);
}

代码示例来源:origin: bonitasoft/bonita-engine

@Test
public void should_build_initial_folder() throws Exception {
  //given
  ConfigurationFolderUtil configurationFolderUtil = new ConfigurationFolderUtil();
  Path setupFolder = temporaryFolder.newFolder().toPath();
  //when
  configurationFolderUtil.buildInitialFolder(setupFolder);
  //then
  final File expectedFolder = setupFolder.resolve(PLATFORM_CONF_FOLDER_NAME).resolve("initial").resolve("platform_init_engine").toFile();
  assertThat(expectedFolder).exists().isDirectory();
  assertThat(expectedFolder.listFiles()).extracting("name").hasSize(1).containsOnly("initialConfig.properties");
}

代码示例来源:origin: sta-szek/pojo-tester

@Test
void Should_Instantiate_Two_Classes() {
  // given
  final Class[] classesToInstantiate = { A.class, B.class };
  // when
  final Object[] result = Instantiable.instantiateClasses(classesToInstantiate, new ArrayListValuedHashMap<>());
  // then
  assertThat(result).extracting(Object::getClass)
           .containsExactlyInAnyOrder(classesToInstantiate);
}

代码示例来源:origin: Microsoft/ApplicationInsights-Java

@Test
public void shouldBeAbleToConfigureInprocessTelemetryChannel() throws IllegalConfigurationException {
  EnvironmentTestUtils.addEnvironment(context,
      "azure.application-insights.instrumentation-key: 00000000-0000-0000-0000-000000000000",
      "azure.application-insights.channel.in-process.developer-mode=false",
      "azure.application-insights.channel.in-process.flush-interval-in-seconds=123",
      "azure.application-insights.channel.in-process.max-telemetry-buffer-capacity=10");
  context.register(PropertyPlaceholderAutoConfiguration.class,
      ApplicationInsightsTelemetryAutoConfiguration.class);
  context.refresh();
  TelemetryConfiguration telemetryConfiguration = context.getBean(TelemetryConfiguration.class);
  TelemetryChannel channel = telemetryConfiguration.getChannel();
  assertThat(channel).isInstanceOf(InProcessTelemetryChannel.class);
  assertThat(channel.isDeveloperMode()).isFalse();
  assertThat(channel).extracting("telemetryBuffer").extracting("transmitBufferTimeoutInSeconds").contains(123);
  assertThat(channel).extracting("telemetryBuffer").extracting("maxTelemetriesInBatch").contains(10);
}

代码示例来源:origin: FaustXVI/junit5-docker

@Test
@DisplayName("start a container with one port")
public void shouldStartContainerWithOnePort() {
  String containerId = defaultDockerClient.startContainer(WANTED_IMAGE, emptyMap(),
    new PortBinding(8081, 8080));
  InspectContainerResponse startedContainer = dockerClient.inspectContainerCmd(containerId).exec();
  Ports ports = startedContainer.getHostConfig().getPortBindings();
  assertThat(ports).isNotNull();
  Map<ExposedPort, Ports.Binding[]> portBindings = ports.getBindings();
  assertThat(portBindings).hasSize(1)
    .containsKeys(new ExposedPort(8080));
  assertThat(portBindings.get(new ExposedPort(8080))).hasSize(1)
    .extracting(Ports.Binding::getHostPortSpec)
    .contains("8081");
}

相关文章