Best Assertj code snippet using org.assertj.core.api.AbstractOptionalAssert.checkNotNull
Source:AbstractOptionalAssert.java
...118 * @return this assertion object.119 */120 public SELF contains(VALUE expectedValue) {121 isNotNull();122 checkNotNull(expectedValue);123 if (!actual.isPresent()) throwAssertionError(shouldContain(expectedValue));124 if (!optionalValueComparisonStrategy.areEqual(actual.get(), expectedValue))125 throwAssertionError(shouldContain(actual, expectedValue));126 return myself;127 }128 /**129 * Verifies that the actual {@link java.util.Optional} contains a value and gives this value to the given130 * {@link java.util.function.Consumer} for further assertions. Should be used as a way of deeper asserting on the131 * containing object, as further requirement(s) for the value.132 * <p>133 * Assertions will pass :134 * <pre><code class='java'> // one requirement 135 * assertThat(Optional.of(10)).hasValueSatisfying(i -> { assertThat(i).isGreaterThan(9); });136 *137 * // multiple requirements138 * assertThat(Optional.of(someString)).hasValueSatisfying(s -> {139 * assertThat(s).isEqualTo("something");140 * assertThat(s).startsWith("some");141 * assertThat(s).endsWith("thing");142 * }); </code></pre>143 *144 * Assertions will fail :145 * <pre><code class='java'> assertThat(Optional.of("something")).hasValueSatisfying(s -> {146 * assertThat(s).isEqualTo("something else");147 * });148 *149 * // fail because optional is empty, there is no value to perform assertion on 150 * assertThat(Optional.empty()).hasValueSatisfying(o -> {});</code></pre>151 *152 * @param requirement to further assert on the object contained inside the {@link java.util.Optional}.153 * @return this assertion object.154 */155 public SELF hasValueSatisfying(Consumer<VALUE> requirement) {156 assertValueIsPresent();157 requirement.accept(actual.get());158 return myself;159 }160 /**161 * Verifies that the actual {@link Optional} contains a value which satisfies the given {@link Condition}.162 * <p>163 * Examples:164 * <pre><code class='java'> Condition<TolkienCharacter> isAnElf = new Condition<>(character -> character.getRace() == ELF, "an elf"); 165 * 166 * TolkienCharacter legolas = new TolkienCharacter("Legolas", 1000, ELF);167 * TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);168 * 169 * // assertion succeeds170 * assertThat(Optional.of(legolas)).hasValueSatisfying(isAnElf);171 * 172 * // assertion fails173 * assertThat(Optional.of(frodo)).hasValueSatisfying(isAnElf);</code></pre>174 *175 * @param condition the given condition.176 * @return this assertion object.177 * @throws AssertionError if the actual {@link Optional} is null or empty.178 * @throws NullPointerException if the given condition is {@code null}.179 * @throws AssertionError if the actual value does not satisfy the given condition.180 * @since 3.6.0181 */182 public SELF hasValueSatisfying(Condition<? super VALUE> condition) {183 assertValueIsPresent();184 conditions.assertIs(info, actual.get(), condition);185 return myself;186 }187 /**188 * Verifies that the actual {@link java.util.Optional} contains the given value (alias of {@link #contains(Object)}).189 * <p>190 * Assertion will pass :191 * <pre><code class='java'> assertThat(Optional.of("something")).hasValue("something");192 * assertThat(Optional.of(10)).contains(10);</code></pre>193 *194 * Assertion will fail :195 * <pre><code class='java'> assertThat(Optional.of("something")).hasValue("something else");196 * assertThat(Optional.of(20)).contains(10);</code></pre>197 *198 * @param expectedValue the expected value inside the {@link java.util.Optional}.199 * @return this assertion object.200 */201 public SELF hasValue(VALUE expectedValue) {202 return contains(expectedValue);203 }204 /**205 * Verifies that the actual {@link Optional} contains a value that is an instance of the argument.206 * <p>207 * Assertions will pass:208 *209 * <pre><code class='java'> assertThat(Optional.of("something")).containsInstanceOf(String.class)210 * .containsInstanceOf(Object.class);211 *212 * assertThat(Optional.of(10)).containsInstanceOf(Integer.class);</code></pre>213 *214 * Assertion will fail:215 *216 * <pre><code class='java'> assertThat(Optional.of("something")).containsInstanceOf(Integer.class);</code></pre>217 *218 * @param clazz the expected class of the value inside the {@link Optional}.219 * @return this assertion object.220 */221 public SELF containsInstanceOf(Class<?> clazz) {222 assertValueIsPresent();223 if (!clazz.isInstance(actual.get())) throwAssertionError(shouldContainInstanceOf(actual, clazz));224 return myself;225 }226 /**227 * Use field/property by field/property comparison (including inherited fields/properties) instead of relying on228 * actual type A <code>equals</code> method to compare the {@link Optional} value's object for incoming assertion229 * checks. Private fields are included but this can be disabled using230 * {@link Assertions#setAllowExtractingPrivateFields(boolean)}.231 * <p>232 * This can be handy if <code>equals</code> method of the {@link Optional} value's object to compare does not suit233 * you.234 * </p>235 * Note that the comparison is <b>not</b> recursive, if one of the fields/properties is an Object, it will be236 * compared to the other field/property using its <code>equals</code> method.237 * <p>238 * Example:239 *240 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);241 * TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);242 *243 * // Fail if equals has not been overridden in TolkienCharacter as equals default implementation only compares references244 * assertThat(Optional.of(frodo)).contains(frodoClone);245 *246 * // frodo and frodoClone are equals when doing a field by field comparison.247 * assertThat(Optional.of(frodo)).usingFieldByFieldValueComparator().contains(frodoClone);</code></pre>248 *249 * @return {@code this} assertion object.250 */251 @CheckReturnValue252 public SELF usingFieldByFieldValueComparator() {253 return usingValueComparator(new FieldByFieldComparator());254 }255 /**256 * Use given custom comparator instead of relying on actual type A <code>equals</code> method to compare the257 * {@link Optional} value's object for incoming assertion checks.258 * <p>259 * Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default260 * comparison strategy.261 * <p>262 * Examples :263 *264 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);265 * TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);266 *267 * // Fail if equals has not been overridden in TolkienCharacter as equals default implementation only compares references268 * assertThat(Optional.of(frodo)).contains(frodoClone);269 *270 * // frodo and frodoClone are equals when doing a field by field comparison.271 * assertThat(Optional.of(frodo)).usingValueComparator(new FieldByFieldComparator()).contains(frodoClone);</code></pre>272 *273 * @param customComparator the comparator to use for incoming assertion checks.274 * @throws NullPointerException if the given comparator is {@code null}.275 * @return {@code this} assertion object.276 */277 @CheckReturnValue278 public SELF usingValueComparator(Comparator<? super VALUE> customComparator) {279 optionalValueComparisonStrategy = new ComparatorBasedComparisonStrategy(customComparator);280 return myself;281 }282 /**283 * Revert to standard comparison for incoming assertion {@link Optional} value checks.284 * <p>285 * This method should be used to disable a custom comparison strategy set by calling286 * {@link #usingValueComparator(Comparator)}.287 *288 * @return {@code this} assertion object.289 */290 @CheckReturnValue291 public SELF usingDefaultValueComparator() {292 // fall back to default strategy to compare actual with other objects.293 optionalValueComparisonStrategy = StandardComparisonStrategy.instance();294 return myself;295 }296 /**297 * Verifies that the actual {@link java.util.Optional} contains the instance given as an argument (i.e. it must be the298 * same instance).299 * <p>300 * Assertion will pass :301 *302 * <pre><code class='java'> String someString = "something";303 * assertThat(Optional.of(someString)).containsSame(someString);304 *305 * // Java will create the same 'Integer' instance when boxing small ints306 * assertThat(Optional.of(10)).containsSame(10);</code></pre>307 *308 * Assertion will fail :309 *310 * <pre><code class='java'> // not even equal:311 * assertThat(Optional.of("something")).containsSame("something else");312 * assertThat(Optional.of(20)).containsSame(10);313 *314 * // equal but not the same: 315 * assertThat(Optional.of(new String("something"))).containsSame(new String("something"));316 * assertThat(Optional.of(new Integer(10))).containsSame(new Integer(10));</code></pre>317 *318 * @param expectedValue the expected value inside the {@link java.util.Optional}.319 * @return this assertion object.320 */321 public SELF containsSame(VALUE expectedValue) {322 isNotNull();323 checkNotNull(expectedValue);324 if (!actual.isPresent()) throwAssertionError(shouldContain(expectedValue));325 if (actual.get() != expectedValue) throwAssertionError(shouldContainSame(actual, expectedValue));326 return myself;327 }328 /**329 * Call {@link Optional#flatMap(Function) flatMap} on the {@code Optional} under test, assertions chained afterwards are performed on the {@code Optional} resulting from the flatMap call.330 * <p>331 * Examples:332 * <pre><code class='java'> Function<String, Optional<String>> UPPER_CASE_OPTIONAL_STRING = 333 * s -> s == null ? Optional.empty() : Optional.of(s.toUpperCase());334 * 335 * // assertions succeed336 * assertThat(Optional.of("something")).contains("something")337 * .flatMap(UPPER_CASE_OPTIONAL_STRING)338 * .contains("SOMETHING");339 * 340 * assertThat(Optional.<String>empty()).flatMap(UPPER_CASE_OPTIONAL_STRING)341 * .isEmpty();342 * 343 * assertThat(Optional.<String>ofNullable(null)).flatMap(UPPER_CASE_OPTIONAL_STRING)344 * .isEmpty();345 * 346 * // assertion fails347 * assertThat(Optional.of("something")).flatMap(UPPER_CASE_OPTIONAL_STRING)348 * .contains("something");</code></pre>349 *350 * @param <U> the type wrapped in the {@link Optional} after the {@link Optional#flatMap(Function) flatMap} operation.351 * @param mapper the {@link Function} to use in the {@link Optional#flatMap(Function) flatMap} operation.352 * @return a new {@link AbstractOptionalAssert} for assertions chaining on the flatMap of the Optional.353 * @throws AssertionError if the actual {@link Optional} is null.354 * @since 3.6.0355 */356 @CheckReturnValue357 public <U> AbstractOptionalAssert<?, U> flatMap(Function<? super VALUE, Optional<U>> mapper) {358 isNotNull();359 return assertThat(actual.flatMap(mapper));360 }361 /**362 * Call {@link Optional#map(Function) map} on the {@code Optional} under test, assertions chained afterwards are performed on the {@code Optional} resulting from the map call.363 * <p>364 * Examples:365 * <pre><code class='java'> // assertions succeed 366 * assertThat(Optional.<String>empty()).map(String::length)367 * .isEmpty();368 * 369 * assertThat(Optional.of("42")).contains("42")370 * .map(String::length)371 * .contains(2);372 * 373 * // assertion fails374 * assertThat(Optional.of("42")).map(String::length)375 * .contains(3);</code></pre>376 *377 * @param <U> the type wrapped in the {@link Optional} after the {@link Optional#map(Function) map} operation.378 * @param mapper the {@link Function} to use in the {@link Optional#map(Function) map} operation.379 * @return a new {@link AbstractOptionalAssert} for assertions chaining on the map of the Optional.380 * @throws AssertionError if the actual {@link Optional} is null.381 * @since 3.6.0382 */383 @CheckReturnValue384 public <U> AbstractOptionalAssert<?, U> map(Function<? super VALUE, ? extends U> mapper) {385 isNotNull();386 return assertThat(actual.map(mapper));387 }388 /**389 * Verifies that the actual {@link Optional} is not {@code null} and not empty and returns an Object assertion 390 * that allows chaining (object) assertions on the optional value.391 * <p>392 * Note that it is only possible to return Object assertions after calling this method due to java generics limitations. 393 * <p>394 * Example:395 * <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);396 * TolkienCharacter sam = new TolkienCharacter("Sam", 38, null);397 *398 * // assertion succeeds since all frodo's fields are set399 * assertThat(Optional.of(frodo)).get().hasNoNullFields();400 *401 * // assertion does not succeed because sam does not have its race set402 * assertThat(Optional.of(sam)).get().hasNoNullFields();</code></pre>403 *404 * @return a new {@link AbstractObjectAssert} for assertions chaining on the value of the Optional.405 * @throws AssertionError if the actual {@link Optional} is null.406 * @throws AssertionError if the actual {@link Optional} is empty.407 * @since 3.9.0408 */409 @CheckReturnValue410 public AbstractObjectAssert<?, VALUE> get() {411 isPresent();412 return assertThat(actual.get());413 }414 private void checkNotNull(Object expectedValue) {415 checkArgument(expectedValue != null, "The expected value should not be <null>.");416 }417 private void assertValueIsPresent() {418 isNotNull();419 if (!actual.isPresent()) throwAssertionError(shouldBePresent(actual));420 }421}...
checkNotNull
Using AI Code Generation
1assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");2assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");3assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");4assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");5assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");6assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");7assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");8assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");9assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");10assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");11assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");12assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");13assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");14assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");15assertThat(Optional.of("foo")).isPresent().get().isEqualTo("foo");16assertThat(Optional
checkNotNull
Using AI Code Generation
1Optional<String> optional = Optional.of("value");2assertThat(optional).isPresent().contains("value");3assertThat(optional).isPresent().contains("value").hasValueSatisfying(value -> assertThat(value).contains("value"));4assertThat(optional).isPresent().hasValueSatisfying(value -> assertThat(value).contains("value"));5Optional<String> optional = Optional.of("value");6assertThat(optional).isPresent().contains("value");7assertThat(optional).isPresent().contains("value").hasValueSatisfying(value -> assertThat(value).contains("value"));8assertThat(optional).isPresent().hasValueSatisfying(value -> assertThat(value).contains("value"));9Optional<String> optional = Optional.of("value");10assertThat(optional).isPresent().contains("value");11assertThat(optional).isPresent().contains("value").hasValueSatisfying(value -> assertThat(value).contains("value"));12assertThat(optional).isPresent().hasValueSatisfying(value -> assertThat(value).contains("value"));13Optional<String> optional = Optional.of("value");14assertThat(optional).isPresent().contains("value");15assertThat(optional).isPresent().contains("value").hasValueSatisfying(value -> assertThat(value).contains("value"));16assertThat(optional).isPresent().hasValueSatisfying(value -> assertThat(value).contains("value"));17Optional<String> optional = Optional.of("value");18assertThat(optional).isPresent().contains("value");19assertThat(optional).isPresent().contains("value").hasValueSatisfying(value -> assertThat(value).contains("value"));20assertThat(optional).isPresent().hasValueSatisfying(value -> assertThat(value).contains("value"));21Optional<String> optional = Optional.of("value");22assertThat(optional).isPresent().contains("value");23assertThat(optional).isPresent().contains("value").hasValueSatisfying(value -> assertThat(value).contains("value"));24assertThat(optional).isPresent().hasValueSatisfying(value -> assertThat(value).contains("value"));
checkNotNull
Using AI Code Generation
1import org.assertj.core.api.AbstractOptionalAssert;2import org.assertj.core.api.Assertions;3public class AssertjOptionalAssertNotNullExample {4 public static void main(String[] args) {5 AbstractOptionalAssert<?, ?> optionalAssert = Assertions.assertThat(Optional.of("Hello World"));6 optionalAssert.isNotNull();7 }8}9 at org.junit.Assert.fail(Assert.java:88)10 at org.junit.Assert.assertTrue(Assert.java:41)11 at org.junit.Assert.assertFalse(Assert.java:64)12 at org.assertj.core.api.AbstractBooleanAssert.isFalse(AbstractBooleanAssert.java:66)13 at org.assertj.core.api.AbstractOptionalAssert.isNotNull(AbstractOptionalAssert.java:88)14 at org.assertj.core.api.AbstractOptionalAssert.isNotNull(AbstractOptionalAssert.java:32)15 at AssertjOptionalAssertNotNullExample.main(AssertjOptionalAssertNotNullExample.java:12)
checkNotNull
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2public class OptionalExample {3 public static void main(String[] args) {4 Optional<String> opt = Optional.of("Hello World!");5 assertThat(opt).isNotEmpty();6 assertThat(opt).isPresent();7 assertThat(opt).isNotNull();8 assertThat(opt).isPresent().contains("Hello World!");9 }10}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!