How to use ShouldOnlyHaveElementsOfTypes class of org.assertj.core.error package

Best Assertj code snippet using org.assertj.core.error.ShouldOnlyHaveElementsOfTypes

Source:ObjectArrays_assertHasOnlyElementsOfTypes_Test.java Github

copy

Full Screen

...11 * Copyright 2012-2019 the original author or authors.12 */13package org.assertj.core.internal.objectarrays;14import org.assertj.core.api.Assertions;15import org.assertj.core.error.ShouldOnlyHaveElementsOfTypes;16import org.assertj.core.internal.ObjectArraysBaseTest;17import org.assertj.core.test.TestData;18import org.assertj.core.util.Arrays;19import org.assertj.core.util.AssertionsUtil;20import org.assertj.core.util.FailureMessages;21import org.assertj.core.util.Lists;22import org.junit.jupiter.api.Test;23public class ObjectArrays_assertHasOnlyElementsOfTypes_Test extends ObjectArraysBaseTest {24 private static final Object[] ARRAY = new Object[]{ 6, 7.0, 8L };25 @Test26 public void should_pass_if_actual_has_only_elements_of_the_expected_types() {27 arrays.assertHasOnlyElementsOfTypes(TestData.someInfo(), ObjectArrays_assertHasOnlyElementsOfTypes_Test.ARRAY, Number.class);28 arrays.assertHasOnlyElementsOfTypes(TestData.someInfo(), ObjectArrays_assertHasOnlyElementsOfTypes_Test.ARRAY, Number.class, Long.class, Integer.class);29 }30 @Test31 public void should_pass_if_actual_has_only_elements_of_the_expected_types_even_if_some_types_dont_match_any_elements() {32 arrays.assertHasOnlyElementsOfTypes(TestData.someInfo(), ObjectArrays_assertHasOnlyElementsOfTypes_Test.ARRAY, Number.class, Long.class, Integer.class, String.class);33 }34 @Test35 public void should_pass_if_actual_and_given_types_are_empty() {36 Class<?>[] types = new Class<?>[0];37 arrays.assertHasOnlyElementsOfTypes(TestData.someInfo(), Arrays.array(), types);38 }39 @Test40 public void should_fail_if_actual_is_null() {41 // GIVEN42 Object[] array = null;43 // GIVEN44 AssertionError error = AssertionsUtil.expectAssertionError(() -> arrays.assertHasOnlyElementsOfTypes(someInfo(), array, .class));45 // THEN46 Assertions.assertThat(error).hasMessage(FailureMessages.actualIsNull());47 }48 @Test49 public void should_fail_if_expected_types_are_empty_but_actual_is_not() {50 // GIVEN51 Class<?>[] types = new Class<?>[0];52 // WHEN53 AssertionError error = AssertionsUtil.expectAssertionError(() -> arrays.assertHasOnlyElementsOfTypes(someInfo(), ARRAY, types));54 // THEN55 Assertions.assertThat(error).hasMessage(ShouldOnlyHaveElementsOfTypes.shouldOnlyHaveElementsOfTypes(ObjectArrays_assertHasOnlyElementsOfTypes_Test.ARRAY, types, Lists.list(ObjectArrays_assertHasOnlyElementsOfTypes_Test.ARRAY)).create());56 }57 @Test58 public void should_fail_if_one_element_in_actual_does_not_belong_to_the_expected_types() {59 // WHEN60 AssertionError error = AssertionsUtil.expectAssertionError(() -> arrays.assertHasOnlyElementsOfTypes(someInfo(), ARRAY, .class, .class));61 // THEN62 Assertions.assertThat(error).hasMessage(ShouldOnlyHaveElementsOfTypes.shouldOnlyHaveElementsOfTypes(ObjectArrays_assertHasOnlyElementsOfTypes_Test.ARRAY, Arrays.array(Long.class, String.class), Lists.list(6, 7.0)).create());63 }64 @Test65 public void should_throw_assertion_error_and_not_null_pointer_exception_on_null_elements() {66 // GIVEN67 Object[] array = Arrays.array(null, "notNull");68 // WHEN69 AssertionError error = AssertionsUtil.expectAssertionError(() -> arrays.assertHasOnlyElementsOfTypes(someInfo(), array, .class));70 // THEN71 Assertions.assertThat(error).hasMessage(ShouldOnlyHaveElementsOfTypes.shouldOnlyHaveElementsOfTypes(array, Arrays.array(Long.class), Lists.list(null, "notNull")).create());72 }73}...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in ShouldOnlyHaveElementsOfTypes

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful