断言 T 子类型的可用性

问题描述 投票:0回答:1

我想断言数组的每个元素是否都是特定类的子类型。所以我写了一个这样的助手:

    private <T> void assertContainsAllSubtypes(
            List<? extends T> list,
            Class<? extends T>... expectedSubtypes) {
        Set<Class<? extends T>> subtypeSet = 
            new HashSet<>(Arrays.asList(expectedSubtypes));

        assertThat(list)
                .isNotEmpty()
                .hasSize(subtypeSet.size())
                .extracting(T::getClass)
                .containsExactlyInAnyOrderElementsOf(subtypeSet);   <-- ERROR
    }

但是我收到以下错误:

Required type:

Iterable
<? extends Class<capture of ? extends T>>

Provided:

Set
<Class<? extends T>>

我错过了什么?

谢谢。

java junit assert
1个回答
0
投票

我相信这会编译:

  private <T, U extends T> void assertContainsAllSubtypes(
      List<T> list,
      Class<U>... expectedSubtypes) {
    Set<Class<U>> subtypeSet =
        new HashSet<>(Arrays.asList(expectedSubtypes));

    assertThat(list)
        .isNotEmpty()
        .hasSize(subtypeSet.size())
        .extracting(T::getClass)
        .containsExactlyInAnyOrderElementsOf(subtypeSet);
  }
© www.soinside.com 2019 - 2024. All rights reserved.