AssertJ - 在检查课程后继续使用流畅的断言

时间:2017-03-09 20:41:21

标签: java fluent assertj

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

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

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

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

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

1 个答案:

答案 0 :(得分:1)

您可以尝试isInstanceOfSatisfying并在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);