How to use PropertyOrFieldSupport_getValueOf_Test class of org.assertj.core.util.introspection package

Best Assertj code snippet using org.assertj.core.util.introspection.PropertyOrFieldSupport_getValueOf_Test

Source:PropertyOrFieldSupport_getValueOf_Test.java Github

copy

Full Screen

...18import org.assertj.core.api.Assertions;19import org.assertj.core.test.Employee;20import org.assertj.core.test.Name;21import org.junit.jupiter.api.Test;22public class PropertyOrFieldSupport_getValueOf_Test {23 private static final Employee yoda = new Employee(1L, new Name("Yoda"), 800);24 private PropertyOrFieldSupport propertyOrFieldSupport;25 @Test26 public void should_extract_property_value() {27 Object value = propertyOrFieldSupport.getValueOf("age", PropertyOrFieldSupport_getValueOf_Test.yoda);28 Assertions.assertThat(value).isEqualTo(800);29 }30 @Test31 public void should_extract_property_with_no_corresponding_field() {32 Object value = propertyOrFieldSupport.getValueOf("adult", PropertyOrFieldSupport_getValueOf_Test.yoda);33 Assertions.assertThat(value).isEqualTo(true);34 }35 @Test36 public void should_prefer_properties_over_fields() {37 Object extractedValue = propertyOrFieldSupport.getValueOf("name", employeeWithOverriddenName("Overridden Name"));38 Assertions.assertThat(extractedValue).isEqualTo(new Name("Overridden Name"));39 }40 @Test41 public void should_extract_public_field_values_as_no_property_matches_given_name() {42 Object value = propertyOrFieldSupport.getValueOf("id", PropertyOrFieldSupport_getValueOf_Test.yoda);43 Assertions.assertThat(value).isEqualTo(1L);44 }45 @Test46 public void should_extract_private_field_values_as_no_property_matches_given_name() {47 Object value = propertyOrFieldSupport.getValueOf("city", PropertyOrFieldSupport_getValueOf_Test.yoda);48 Assertions.assertThat(value).isEqualTo("New York");49 }50 @Test51 public void should_fallback_to_field_if_exception_has_been_thrown_on_property_access() {52 Object extractedValue = propertyOrFieldSupport.getValueOf("name", employeeWithBrokenName("Name"));53 Assertions.assertThat(extractedValue).isEqualTo(new Name("Name"));54 }55 @Test56 public void should_return_null_if_one_of_nested_property_or_field_value_is_null() {57 Object value = propertyOrFieldSupport.getValueOf("surname.first", PropertyOrFieldSupport_getValueOf_Test.yoda);58 Assertions.assertThat(value).isNull();59 }60 @Test61 public void should_extract_nested_property_field_combinations() {62 Employee darth = new Employee(1L, new Name("Darth", "Vader"), 100);63 Employee luke = new Employee(2L, new Name("Luke", "Skywalker"), 26);64 darth.field = luke;65 luke.field = darth;66 luke.surname = new Name("Young", "Padawan");67 Object value = propertyOrFieldSupport.getValueOf("me.field.me.field.me.field.surname.name", darth);68 Assertions.assertThat(value).isEqualTo("Young Padawan");69 }70 @Test71 public void should_throw_error_when_no_property_nor_field_match_given_name() {72 Assertions.assertThatExceptionOfType(IntrospectionError.class).isThrownBy(() -> propertyOrFieldSupport.getValueOf("unknown", yoda));73 }74 @Test75 public void should_throw_error_when_no_property_nor_public_field_match_given_name_if_extraction_is_limited_to_public_fields() {76 Assertions.assertThatExceptionOfType(IntrospectionError.class).isThrownBy(() -> {77 propertyOrFieldSupport = new PropertyOrFieldSupport(new PropertySupport(), FieldSupport.EXTRACTION_OF_PUBLIC_FIELD_ONLY);78 propertyOrFieldSupport.getValueOf("city", yoda);79 });80 }81 @Test82 public void should_throw_exception_when_given_property_or_field_name_is_null() {83 Assertions.assertThatIllegalArgumentException().isThrownBy(() -> propertyOrFieldSupport.getValueOf(null, yoda)).withMessage("The name of the property/field to read should not be null");84 }85 @Test86 public void should_throw_exception_when_given_name_is_empty() {87 Assertions.assertThatIllegalArgumentException().isThrownBy(() -> propertyOrFieldSupport.getValueOf("", yoda)).withMessage("The name of the property/field to read should not be empty");88 }89 @Test90 public void should_throw_exception_if_property_cannot_be_extracted_due_to_runtime_exception_during_property_access() {91 Assertions.assertThatExceptionOfType(IntrospectionError.class).isThrownBy(() -> propertyOrFieldSupport.getValueOf("adult", brokenEmployee()));92 }93 @Test94 public void should_throw_exception_if_no_object_is_given() {95 Assertions.assertThatIllegalArgumentException().isThrownBy(() -> propertyOrFieldSupport.getValueOf("name", null));96 }97 @Test98 public void should_extract_single_value_from_maps_by_key() {99 String key1 = "key1";100 String key2 = "key2";101 Map<String, Employee> map1 = new HashMap<>();102 map1.put(key1, PropertyOrFieldSupport_getValueOf_Test.yoda);103 Employee luke = new Employee(2L, new Name("Luke"), 22);104 map1.put(key2, luke);105 Map<String, Employee> map2 = new HashMap<>();106 map2.put(key1, PropertyOrFieldSupport_getValueOf_Test.yoda);107 Employee han = new Employee(3L, new Name("Han"), 31);108 map2.put(key2, han);109 List<Map<String, Employee>> maps = Arrays.asList(map1, map2);110 Assertions.assertThat(maps).extracting(key2).containsExactly(luke, han);111 Assertions.assertThat(maps).extracting(key2, Employee.class).containsExactly(luke, han);112 Assertions.assertThat(maps).extracting(key1).containsExactly(PropertyOrFieldSupport_getValueOf_Test.yoda, PropertyOrFieldSupport_getValueOf_Test.yoda);113 Assertions.assertThat(maps).extracting("bad key").containsExactly(null, null);114 }115}...

Full Screen

Full Screen

PropertyOrFieldSupport_getValueOf_Test

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.assertThatExceptionOfType;3import static org.assertj.core.util.introspection.PropertyOrFieldSupport.EXTRACTION;4import org.assertj.core.util.introspection.PropertyOrFieldSupport;5import org.junit.jupiter.api.Test;6public class PropertyOrFieldSupport_getValueOf_Test {7 public void should_get_value_of_property() {8 Person person = new Person("John", 32);9 assertThat(EXTRACTION.getValueOf("name", person)).isEqualTo("John");10 assertThat(EXTRACTION.getValueOf("age", person)).isEqualTo(32);11 }12 public void should_get_value_of_field() {13 Person person = new Person("John", 32);14 assertThat(EXTRACTION.getValueOf("nickname", person)).isEqualTo("John");15 assertThat(EXTRACTION.getValueOf("years", person)).isEqualTo(32);16 }17 public void should_get_value_of_public_field() {18 Person person = new Person("John", 32);19 assertThat(EXTRACTION.getValueOf("city", person)).isEqualTo("New York");20 }21 public void should_throw_error_because_property_or_field_does_not_exist() {22 assertThatExceptionOfType(IntrospectionError.class).isThrownBy(() -> EXTRACTION.getValueOf("unknown", new Person("John", 32)))23 .withMessageContaining("Can't find any field or property with name 'unknown' in");24 }25 public void should_throw_error_because_property_or_field_is_not_readable() {26 assertThatExceptionOfType(IntrospectionError.class).isThrownBy(() -> EXTRACTION.getValueOf("writeOnly", new Person("John", 32)))27 .withMessageContaining("Can't read field or property with name 'writeOnly' in");28 }29 public void should_throw_error_because_property_or_field_is_not_public() {30 assertThatExceptionOfType(IntrospectionError.class).isThrownBy(() -> EXTRACTION.getValueOf("privateField", new Person("John", 32)))31 .withMessageContaining("Can't read field or property with name 'privateField' in");32 }33 private static class Person {34 public String name;35 public int age;36 public String city = "New York";37 private String privateField = "privateField";38 public String writeOnly;39 public Person(String name,

Full Screen

Full Screen

PropertyOrFieldSupport_getValueOf_Test

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.util.introspection.PropertyOrFieldSupport;2import static org.assertj.core.api.Assertions.assertThat;3import java.util.List;4import org.assertj.core.util.introspection.PropertyOrFieldSupport;5import static org.assertj.core.api.Assertions.assertThat;6import java.util.List;7/** @author fgruchala */ public class PropertyOrFieldSupport_getValueOf_Test {8private PropertyOrFieldSupport propertyOrFieldSupport = new PropertyOrFieldSupport();

Full Screen

Full Screen

PropertyOrFieldSupport_getValueOf_Test

Using AI Code Generation

copy

Full Screen

1public static Object getValueOf(String name, Object target) {2 if (name == null || name.isEmpty()) {3 throw new IllegalArgumentException("The name of the property/field to read should not be null or empty");4 }5 if (target == null) {6 throw new IllegalArgumentException("The target on which to read the property/field should not be null");7 }8 if (target instanceof Class) {9 throw new IllegalArgumentException("The target on which to read the property/field should not be a Class, but an instance.");10 }11 if (target.getClass().isArray()) {12 throw new IllegalArgumentException("The target on which to read the property/field should not be an array, but an instance.");13 }14 if (target instanceof Boolean || target instanceof Character || target instanceof Byte || target instanceof Short15 || target instanceof Integer || target instanceof Long || target instanceof Float || target instanceof Double) {16 throw new IllegalArgumentException("The target on which to read the property/field should not be a primitive, but an instance.");17 }18 if (target instanceof String) {19 throw new IllegalArgumentException("The target on which to read the property/field should not be a String, but an instance.");20 }21 if (target instanceof Date) {22 throw new IllegalArgumentException("The target on

Full Screen

Full Screen

PropertyOrFieldSupport_getValueOf_Test

Using AI Code Generation

copy

Full Screen

1| `should_return_value_of_public_field()` | should return value of public field |2| `should_return_value_of_public_property()` | should return value of public property |3| `should_return_value_of_public_property_with_underscore()` | should return value of public property with underscore |4| `should_return_value_of_public_property_with_underscore_and_capital_letter()` | should return value of public property with underscore and capital letter |5| `should_return_value_of_public_property_with_underscore_and_capital_letters()` | should return value of public property with underscore and capital letters |6| `should_return_value_of_public_property_with_capital_letters()` | should return value of public property with capital letters |7| `should_return_value_of_public_property_with_capital_letter()` | should return value of public property with capital letter |8| `should_return_value_of_public_property_with_capital_letter_and_underscore()` | should return value of public property with capital letter and underscore |9| `should_return_value_of_public_property_with_capital_letters_and_underscore()` | should return value of public property with capital letters and underscore |10| `should_return_value_of_public_property_with_capital_letters_and_underscore_and_capital_letters()` | should return value of public property with capital letters and underscore and capital letters |11| `should_return_value_of_public_property_with_capital_letters_and_underscore_and_capital_letter()` | should return value of public property with capital letters and underscore and capital letter |12| `should_return_value_of_public_property_with_capital_letter_and_underscore_and_capital_letters()` | should return value of public property with capital letter and underscore and capital letters |13| `should_return_value_of_public_property_with_capital_letter_and_underscore_and_capital_letter()` | should return value of public property with capital letter and underscore and capital letter |14| `should_return_value_of_public_property_with_underscore_and_capital_letters_and_underscore()` | should return value of public property with underscore and capital letters and underscore |15| `should_return_value_of_public_property_with_underscore_and_capital_letters_and_underscore_and_capital_letters()` | should return value of public property with underscore and capital letters and underscore and capital letters |16| `should_return_value_of_public_property_with_underscore_and_capital_letters_and_underscore_and_capital_letter()` | should return value of public property with underscore and capital letters and underscore and capital letter |

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 PropertyOrFieldSupport_getValueOf_Test

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