1

假设我有一个Map<String, Action>,我会这样:

    assertThat( spyActionMap.get( "a" ) ).isInstanceOf( Action.class );

...通过。现在我想检查Action获得的是正确的:

    assertThat( spyActionMap.get( "a" ) ).isInstanceOf( Action.class ).getValue( Action.NAME ).isEqualTo( "Go crazy" );

...不编译,不足为奇。有没有办法做这种事情?

4

1 回答 1

3

您可以尝试isInstanceOfSatisfying并在 a 中指定您的断言Consumer

Object yoda = new Jedi("Yoda", "Green");
Object luke = new Jedi("Luke Skywalker", "Green");

Consumer<Jedi> jediRequirements = jedi -> {
   assertThat(jedi.getLightSaberColor()).isEqualTo("Green");
   assertThat(jedi.getName()).doesNotContain("Dark");
};

assertThat(yoda).isInstanceOfSatisfying(Jedi.class, jediRequirements);
assertThat(luke).isInstanceOfSatisfying(Jedi.class, jediRequirements);
于 2017-03-10T00:11:43.700 回答