当前位置: 首页 > 知识库问答 >
问题:

在AssertJ中测试一个事物是否成立

郑波
2023-03-14
assertThat(list, either(contains(Tags.SWEETS, Tags.HIGH))
    .or(contains(Tags.SOUPS, Tags.RED)));

共有1个答案

柯易安
2023-03-14

编辑:

由于3.12.0 AssertJ提供SatisfieSanyOf,成功是给定断言成功之一,

assertThat(list).satisfiesAnyOf(
    listParam -> assertThat(listParam).contains(Tags.SWEETS, Tags.HIGH),
    listParam -> assertThat(listParam).contains(Tags.SOUPS, Tags.RED)
);

原文答案:

Set<String> goodTags = newLinkedHashSet("Fine", "Good");
Set<String> badTags = newLinkedHashSet("Bad!", "Awful");
Set<String> tags = newLinkedHashSet("Fine", "Good", "Ok", "?");

// contains is statically imported from ContainsCondition
// anyOf succeeds if one of the conditions is met (logical 'or') 
assertThat(tags).has(anyOf(contains(goodTags), contains(badTags)));
import static org.assertj.core.util.Lists.newArrayList;    
import java.util.Collection;    
import org.assertj.core.api.Condition;

public class ContainsCondition extends Condition<Iterable<String>> {
  private Collection<String> collection;

  public ContainsCondition(Iterable<String> values) {
    super("contains " + values);
    this.collection = newArrayList(values);
  }

  static ContainsCondition contains(Collection<String> set) {
    return new ContainsCondition(set);
  }

  @Override
  public boolean matches(Iterable<String> actual) {
    Collection<String> values = newArrayList(actual);
    for (String string : collection) {
      if (!values.contains(string)) return false;
    }
    return true;
  };
}
 类似资料: