295 lines
18 KiB
Java
295 lines
18 KiB
Java
import org.assertj.core.api.Condition;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.function.Consumer;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.fail;
|
|
import static org.assertj.guava.api.Assertions.assertThat;
|
|
|
|
public class ImplicitAssertion {
|
|
|
|
private void implicitAssertion() {
|
|
List<String> list = new ArrayList<>();
|
|
List<String> moreStrings = new ArrayList<>();
|
|
Condition<String> stringCondition = new Condition<>();
|
|
Condition<List<? extends String>> listCondition = new Condition<>();
|
|
Consumer<String> stringConsumer = (it) -> {
|
|
};
|
|
|
|
String string = "foo";
|
|
|
|
com.google.common.base.Optional<String> guavaOptional = com.google.common.base.Optional.absent();
|
|
Optional<String> java8Optional = Optional.empty();
|
|
|
|
assertThat(string).isNotNull().isNullOrEmpty(); // could be equal to .isEmpty()
|
|
assertThat(string).isNotNull().isEmpty();
|
|
assertThat(string).isNotNull().isNotEmpty();
|
|
assertThat(string).isNotNull().hasSize(1);
|
|
assertThat(string).isNotNull().hasSizeGreaterThan(1);
|
|
assertThat(string).isNotNull().hasSizeGreaterThanOrEqualTo(1);
|
|
assertThat(string).isNotNull().hasSizeLessThan(1);
|
|
assertThat(string).isNotNull().hasSizeLessThanOrEqualTo(1);
|
|
assertThat(string).isNotNull().hasSizeBetween(1, 2);
|
|
assertThat(string).isNotNull().hasSameSizeAs(moreStrings);
|
|
assertThat(string).isNotNull().isEqualTo("bar");
|
|
assertThat(string).isNotNull().isNotEqualTo("bar");
|
|
assertThat(string).isNotNull().isSameAs("bar");
|
|
assertThat(string).isNotNull().isNotSameAs("bar");
|
|
assertThat(string).isNotNull().contains(string);
|
|
assertThat(string).isNotNull().containsOnlyOnce(string);
|
|
assertThat(string).isNotNull().isIn(string, "bar");
|
|
assertThat(string).isNotNull().isNotIn(string, "bar");
|
|
assertThat(string).isNotNull().doesNotContain(string, "bar");
|
|
assertThat(string).isNotNull().startsWith("bar");
|
|
assertThat(string).isNotNull().endsWith("bar");
|
|
assertThat(string).isNotNull().containsSequence(moreStrings);
|
|
assertThat(string).isNotNull().containsSubsequence(moreStrings);
|
|
assertThat(string).isNotNull().has(stringCondition);
|
|
assertThat(string).isNotNull().doesNotHave(stringCondition);
|
|
assertThat(string).isNotNull().is(stringCondition);
|
|
assertThat(string).isNotNull().isNot(stringCondition);
|
|
assertThat(string).isNotNull().hasToString(string);
|
|
assertThat(string).isNotNull().isInstanceOf(String.class);
|
|
assertThat(string).isNotNull().isNotInstanceOf(String.class);
|
|
assertThat(string).isNotNull().isExactlyInstanceOf(String.class);
|
|
assertThat(string).isNotNull().isNotExactlyInstanceOf(String.class);
|
|
assertThat(string).isNotNull().isInstanceOfAny(String.class);
|
|
assertThat(string).isNotNull().isNotInstanceOfAny(String.class);
|
|
assertThat(string).isNotNull().isInstanceOfSatisfying(String.class, stringConsumer);
|
|
assertThat(string).isNotNull().hasSameClassAs("bar");
|
|
assertThat(string).isNotNull().doesNotHaveSameClassAs("bar");
|
|
assertThat(string).isNotNull().isOfAnyClassIn(String.class, CharSequence.class);
|
|
assertThat(string).isNotNull().isNotOfAnyClassIn(String.class, CharSequence.class);
|
|
|
|
assertThat(string).isNotEmpty().isNullOrEmpty();
|
|
assertThat(string).isNotEmpty().isEmpty();
|
|
assertThat(string).isNotEmpty().isNotEmpty();
|
|
assertThat(string).isNotEmpty().hasSize(1);
|
|
assertThat(string).isNotEmpty().hasSizeGreaterThan(1);
|
|
assertThat(string).isNotEmpty().hasSizeGreaterThanOrEqualTo(1);
|
|
assertThat(string).isNotEmpty().hasSizeLessThan(1);
|
|
assertThat(string).isNotEmpty().hasSizeLessThanOrEqualTo(1);
|
|
assertThat(string).isNotEmpty().hasSizeBetween(1, 2);
|
|
assertThat(string).isNotEmpty().hasSameSizeAs(moreStrings);
|
|
assertThat(string).isNotEmpty().isEqualTo("bar");
|
|
assertThat(string).isNotEmpty().isNotEqualTo("bar");
|
|
assertThat(string).isNotEmpty().isSameAs("bar");
|
|
assertThat(string).isNotEmpty().isNotSameAs("bar");
|
|
assertThat(string).isNotEmpty().contains(string);
|
|
assertThat(string).isNotEmpty().containsOnlyOnce(string);
|
|
assertThat(string).isNotEmpty().isIn(string, "bar");
|
|
assertThat(string).isNotEmpty().isNotIn(string, "bar");
|
|
assertThat(string).isNotEmpty().doesNotContain(string, "bar");
|
|
assertThat(string).isNotEmpty().startsWith("bar");
|
|
assertThat(string).isNotEmpty().endsWith("bar");
|
|
assertThat(string).isNotEmpty().containsSequence(moreStrings);
|
|
assertThat(string).isNotEmpty().containsSubsequence(moreStrings);
|
|
assertThat(string).isNotEmpty().has(stringCondition);
|
|
assertThat(string).isNotEmpty().doesNotHave(stringCondition);
|
|
assertThat(string).isNotEmpty().is(stringCondition);
|
|
assertThat(string).isNotEmpty().isNot(stringCondition);
|
|
assertThat(string).isNotEmpty().hasToString(string);
|
|
assertThat(string).isNotEmpty().isInstanceOf(String.class);
|
|
assertThat(string).isNotEmpty().isNotInstanceOf(String.class);
|
|
assertThat(string).isNotEmpty().isExactlyInstanceOf(String.class);
|
|
assertThat(string).isNotEmpty().isNotExactlyInstanceOf(String.class);
|
|
assertThat(string).isNotEmpty().isInstanceOfAny(String.class);
|
|
assertThat(string).isNotEmpty().isNotInstanceOfAny(String.class);
|
|
assertThat(string).isNotEmpty().isInstanceOfSatisfying(String.class, stringConsumer);
|
|
assertThat(string).isNotEmpty().hasSameClassAs("bar");
|
|
assertThat(string).isNotEmpty().doesNotHaveSameClassAs("bar");
|
|
assertThat(string).isNotEmpty().isOfAnyClassIn(String.class, CharSequence.class);
|
|
assertThat(string).isNotEmpty().isNotOfAnyClassIn(String.class, CharSequence.class);
|
|
|
|
assertThat(list).isNotNull().isNullOrEmpty();
|
|
assertThat(list).isNotNull().isEmpty();
|
|
assertThat(list).isNotNull().isNotEmpty();
|
|
assertThat(list).isNotNull().hasSize(1);
|
|
assertThat(list).isNotNull().hasSizeGreaterThan(1);
|
|
assertThat(list).isNotNull().hasSizeGreaterThanOrEqualTo(1);
|
|
assertThat(list).isNotNull().hasSizeLessThan(1);
|
|
assertThat(list).isNotNull().hasSizeLessThanOrEqualTo(1);
|
|
assertThat(list).isNotNull().hasSizeBetween(1, 2);
|
|
assertThat(list).isNotNull().hasSameSizeAs(moreStrings);
|
|
assertThat(list).isNotNull().isEqualTo(moreStrings);
|
|
assertThat(list).isNotNull().isNotEqualTo(moreStrings);
|
|
assertThat(list).isNotNull().isSameAs(moreStrings);
|
|
assertThat(list).isNotNull().isNotSameAs(moreStrings);
|
|
assertThat(list).isNotNull().contains(string);
|
|
assertThat(list).isNotNull().containsOnly(string);
|
|
assertThat(list).isNotNull().containsOnlyOnce(string);
|
|
assertThat(list).isNotNull().containsOnlyNulls();
|
|
assertThat(list).isNotNull().doesNotContainNull();
|
|
assertThat(list).isNotNull().containsExactly(string, "bar");
|
|
assertThat(list).isNotNull().containsExactlyInAnyOrder(string, "bar");
|
|
assertThat(list).isNotNull().containsAnyOf(string, "bar");
|
|
assertThat(list).isNotNull().containsAll(moreStrings);
|
|
assertThat(list).isNotNull().containsExactlyInAnyOrderElementsOf(moreStrings);
|
|
assertThat(list).isNotNull().isIn(string, "bar");
|
|
assertThat(list).isNotNull().isNotIn(string, "bar");
|
|
assertThat(list).isNotNull().doesNotContain(string, "bar");
|
|
assertThat(list).isNotNull().hasSameElementsAs(moreStrings);
|
|
assertThat(list).isNotNull().containsAnyElementsOf(moreStrings);
|
|
assertThat(list).isNotNull().containsExactlyElementsOf(moreStrings);
|
|
assertThat(list).isNotNull().containsOnlyElementsOf(moreStrings);
|
|
assertThat(list).isNotNull().doesNotContainAnyElementsOf(moreStrings);
|
|
assertThat(list).isNotNull().isSubsetOf(moreStrings);
|
|
assertThat(list).isNotNull().startsWith(string, "bar");
|
|
assertThat(list).isNotNull().endsWith(string, "bar");
|
|
assertThat(list).isNotNull().doesNotHaveDuplicates();
|
|
assertThat(list).isNotNull().containsSequence(moreStrings);
|
|
assertThat(list).isNotNull().containsSubsequence(moreStrings);
|
|
assertThat(list).isNotNull().doesNotContainSequence(moreStrings);
|
|
assertThat(list).isNotNull().doesNotContainSubsequence(moreStrings);
|
|
assertThat(list).isNotNull().are(stringCondition);
|
|
assertThat(list).isNotNull().areNot(stringCondition);
|
|
assertThat(list).isNotNull().have(stringCondition);
|
|
assertThat(list).isNotNull().doNotHave(stringCondition);
|
|
assertThat(list).isNotNull().areAtLeastOne(stringCondition);
|
|
assertThat(list).isNotNull().areAtLeast(1, stringCondition);
|
|
assertThat(list).isNotNull().areAtMost(1, stringCondition);
|
|
assertThat(list).isNotNull().areExactly(1, stringCondition);
|
|
assertThat(list).isNotNull().hasOnlyOneElementSatisfying(stringConsumer);
|
|
assertThat(list).isNotNull().singleElement(as(STRING)).isEqualTo(stringConsumer);
|
|
assertThat(list).isNotNull().haveAtLeastOne(stringCondition);
|
|
assertThat(list).isNotNull().haveAtLeast(1, stringCondition);
|
|
assertThat(list).isNotNull().haveAtMost(1, stringCondition);
|
|
assertThat(list).isNotNull().haveExactly(1, stringCondition);
|
|
assertThat(list).isNotNull().hasAtLeastOneElementOfType(String.class);
|
|
assertThat(list).isNotNull().hasOnlyElementsOfType(String.class);
|
|
assertThat(list).isNotNull().hasOnlyElementsOfTypes(String.class);
|
|
assertThat(list).isNotNull().doesNotHaveAnyElementsOfTypes(String.class);
|
|
assertThat(list).isNotNull().hasOnlyElementsOfTypes(String.class);
|
|
assertThat(list).isNotNull().has(listCondition);
|
|
assertThat(list).isNotNull().doesNotHave(listCondition);
|
|
assertThat(list).isNotNull().is(listCondition);
|
|
assertThat(list).isNotNull().isNot(listCondition);
|
|
assertThat(list).isNotNull().hasToString(string);
|
|
assertThat(list).isNotNull().isInstanceOf(String.class);
|
|
assertThat(list).isNotNull().isNotInstanceOf(String.class);
|
|
assertThat(list).isNotNull().isExactlyInstanceOf(String.class);
|
|
assertThat(list).isNotNull().isNotExactlyInstanceOf(String.class);
|
|
assertThat(list).isNotNull().isInstanceOfAny(String.class);
|
|
assertThat(list).isNotNull().isNotInstanceOfAny(String.class);
|
|
assertThat(list).isNotNull().noneMatch(it -> true);
|
|
assertThat(list).isNotNull().isInstanceOfSatisfying(String.class, stringConsumer);
|
|
assertThat(list).isNotNull().hasSameClassAs("bar");
|
|
assertThat(list).isNotNull().doesNotHaveSameClassAs("bar");
|
|
assertThat(list).isNotNull().isOfAnyClassIn(String.class, CharSequence.class);
|
|
assertThat(list).isNotNull().isNotOfAnyClassIn(String.class, CharSequence.class);
|
|
|
|
assertThat(list).isNotEmpty().isNullOrEmpty();
|
|
assertThat(list).isNotEmpty().isEmpty();
|
|
assertThat(list).isNotEmpty().isNotEmpty();
|
|
assertThat(list).isNotEmpty().hasSize(1);
|
|
assertThat(list).isNotEmpty().hasSizeGreaterThan(1);
|
|
assertThat(list).isNotEmpty().hasSizeGreaterThanOrEqualTo(1);
|
|
assertThat(list).isNotEmpty().hasSizeLessThan(1);
|
|
assertThat(list).isNotEmpty().hasSizeLessThanOrEqualTo(1);
|
|
assertThat(list).isNotEmpty().hasSizeBetween(1, 2);
|
|
assertThat(list).isNotEmpty().hasSameSizeAs(moreStrings);
|
|
assertThat(list).isNotEmpty().isEqualTo(moreStrings);
|
|
assertThat(list).isNotEmpty().isNotEqualTo(moreStrings);
|
|
assertThat(list).isNotEmpty().isSameAs(moreStrings);
|
|
assertThat(list).isNotEmpty().isNotSameAs(moreStrings);
|
|
assertThat(list).isNotEmpty().contains(string);
|
|
assertThat(list).isNotEmpty().containsOnly(string);
|
|
assertThat(list).isNotEmpty().containsOnlyOnce(string);
|
|
assertThat(list).isNotEmpty().containsOnlyNulls();
|
|
assertThat(list).isNotEmpty().doesNotContainNull();
|
|
assertThat(list).isNotEmpty().containsExactly(string, "bar");
|
|
assertThat(list).isNotEmpty().containsExactlyInAnyOrder(string, "bar");
|
|
assertThat(list).isNotEmpty().containsAnyOf(string, "bar");
|
|
assertThat(list).isNotEmpty().containsAll(moreStrings);
|
|
assertThat(list).isNotEmpty().containsExactlyInAnyOrderElementsOf(moreStrings);
|
|
assertThat(list).isNotEmpty().isIn(string, "bar");
|
|
assertThat(list).isNotEmpty().isNotIn(string, "bar");
|
|
assertThat(list).isNotEmpty().doesNotContain(string, "bar");
|
|
assertThat(list).isNotEmpty().hasSameElementsAs(moreStrings);
|
|
assertThat(list).isNotEmpty().containsAnyElementsOf(moreStrings);
|
|
assertThat(list).isNotEmpty().containsExactlyElementsOf(moreStrings);
|
|
assertThat(list).isNotEmpty().containsOnlyElementsOf(moreStrings);
|
|
assertThat(list).isNotEmpty().doesNotContainAnyElementsOf(moreStrings);
|
|
assertThat(list).isNotEmpty().isSubsetOf(moreStrings);
|
|
assertThat(list).isNotEmpty().startsWith(string, "bar");
|
|
assertThat(list).isNotEmpty().endsWith(string, "bar");
|
|
assertThat(list).isNotEmpty().doesNotHaveDuplicates();
|
|
assertThat(list).isNotEmpty().containsSequence(moreStrings);
|
|
assertThat(list).isNotEmpty().containsSubsequence(moreStrings);
|
|
assertThat(list).isNotEmpty().doesNotContainSequence(moreStrings);
|
|
assertThat(list).isNotEmpty().doesNotContainSubsequence(moreStrings);
|
|
assertThat(list).isNotEmpty().are(stringCondition);
|
|
assertThat(list).isNotEmpty().areNot(stringCondition);
|
|
assertThat(list).isNotEmpty().have(stringCondition);
|
|
assertThat(list).isNotEmpty().doNotHave(stringCondition);
|
|
assertThat(list).isNotEmpty().areAtLeastOne(stringCondition);
|
|
assertThat(list).isNotEmpty().areAtLeast(1, stringCondition);
|
|
assertThat(list).isNotEmpty().areAtMost(1, stringCondition);
|
|
assertThat(list).isNotEmpty().areExactly(1, stringCondition);
|
|
assertThat(list).isNotEmpty().hasOnlyOneElementSatisfying(stringConsumer);
|
|
assertThat(list).isNotEmpty().singleElement(as(STRING)).isEqualTo(stringConsumer);
|
|
assertThat(list).isNotEmpty().haveAtLeastOne(stringCondition);
|
|
assertThat(list).isNotEmpty().haveAtLeast(1, stringCondition);
|
|
assertThat(list).isNotEmpty().haveAtMost(1, stringCondition);
|
|
assertThat(list).isNotEmpty().haveExactly(1, stringCondition);
|
|
assertThat(list).isNotEmpty().hasAtLeastOneElementOfType(String.class);
|
|
assertThat(list).isNotEmpty().hasOnlyElementsOfType(String.class);
|
|
assertThat(list).isNotEmpty().hasOnlyElementsOfTypes(String.class);
|
|
assertThat(list).isNotEmpty().doesNotHaveAnyElementsOfTypes(String.class);
|
|
assertThat(list).isNotEmpty().hasOnlyElementsOfTypes(String.class);
|
|
assertThat(list).isNotEmpty().has(listCondition);
|
|
assertThat(list).isNotEmpty().doesNotHave(listCondition);
|
|
assertThat(list).isNotEmpty().is(listCondition);
|
|
assertThat(list).isNotEmpty().isNot(listCondition);
|
|
assertThat(list).isNotEmpty().hasToString(string);
|
|
assertThat(list).isNotEmpty().isInstanceOf(String.class);
|
|
assertThat(list).isNotEmpty().isNotInstanceOf(String.class);
|
|
assertThat(list).isNotEmpty().isExactlyInstanceOf(String.class);
|
|
assertThat(list).isNotEmpty().isNotExactlyInstanceOf(String.class);
|
|
assertThat(list).isNotEmpty().isInstanceOfAny(String.class);
|
|
assertThat(list).isNotEmpty().isNotInstanceOfAny(String.class);
|
|
assertThat(list).isNotEmpty().noneMatch(it -> true);
|
|
assertThat(list).isNotEmpty().isInstanceOfSatisfying(String.class, stringConsumer);
|
|
assertThat(list).isNotEmpty().hasSameClassAs("bar");
|
|
assertThat(list).isNotEmpty().doesNotHaveSameClassAs("bar");
|
|
assertThat(list).isNotEmpty().isOfAnyClassIn(String.class, CharSequence.class);
|
|
assertThat(list).isNotEmpty().isNotOfAnyClassIn(String.class, CharSequence.class);
|
|
|
|
|
|
assertThat(java8Optional).isNotNull().isPresent();
|
|
assertThat(java8Optional).isNotNull().isNotPresent();
|
|
assertThat(java8Optional).isNotNull().isEmpty();
|
|
assertThat(java8Optional).isNotNull().isNotEmpty();
|
|
assertThat(java8Optional).isNotNull().contains("foo");
|
|
assertThat(java8Optional).isNotNull().containsSame("foo");
|
|
assertThat(java8Optional).isNotNull().hasValue("foo");
|
|
assertThat(java8Optional).isNotNull().containsInstanceOf(String.class);
|
|
assertThat(java8Optional).isNotNull().hasValueSatisfying(stringCondition);
|
|
assertThat(java8Optional).isPresent().contains("foo");
|
|
assertThat(java8Optional).isPresent().containsSame("foo");
|
|
assertThat(java8Optional).isPresent().hasValue("foo");
|
|
assertThat(java8Optional).isPresent().containsInstanceOf(String.class);
|
|
assertThat(java8Optional).isPresent().hasValueSatisfying(stringCondition);
|
|
assertThat(java8Optional).isNotEmpty().contains("foo");
|
|
assertThat(java8Optional).isNotEmpty().containsSame("foo");
|
|
assertThat(java8Optional).isNotEmpty().hasValue("foo");
|
|
assertThat(java8Optional).isNotEmpty().containsInstanceOf(String.class);
|
|
assertThat(java8Optional).isNotEmpty().hasValueSatisfying(stringCondition);
|
|
|
|
assertThat(guavaOptional).isNotNull().isPresent();
|
|
assertThat(guavaOptional).isNotNull().isAbsent();
|
|
assertThat(guavaOptional).isNotNull().contains("foo");
|
|
assertThat(guavaOptional).isNotNull().extractingValue().isEqualTo("foo");
|
|
assertThat(guavaOptional).isNotNull().extractingCharSequence().isEqualTo("foo");
|
|
assertThat(guavaOptional).isPresent().contains("foo");
|
|
assertThat(guavaOptional).isPresent().extractingValue().isEqualTo("foo");
|
|
assertThat(guavaOptional).isPresent().extractingCharSequence().isEqualTo("foo");
|
|
|
|
org.junit.Assert.assertThat(string, null);
|
|
fail("oh no!");
|
|
}
|
|
}
|