How to use ByNameSingleExtractor class of org.assertj.core.extractor package

Best Assertj code snippet using org.assertj.core.extractor.ByNameSingleExtractor

Source:ByNameSingleExtractorTest.java Github

copy

Full Screen

...22import org.assertj.core.util.introspection.Introspection;23import org.assertj.core.util.introspection.IntrospectionError;24import org.junit.jupiter.api.DisplayName;25import org.junit.jupiter.api.Test;26@DisplayName("ByNameSingleExtractor")27class ByNameSingleExtractorTest {28 private static final Employee YODA = new Employee(1L, new Name("Yoda"), 800);29 @Test30 void should_extract_field_values_even_if_property_does_not_exist() {31 // GIVEN32 ByNameSingleExtractor underTest = new ByNameSingleExtractor("id");33 // WHEN34 Object result = underTest.apply(YODA);35 // THEN36 then(result).isEqualTo(1L);37 }38 @Test39 void should_extract_property_values_when_no_public_field_match_given_name() {40 // GIVEN41 ByNameSingleExtractor underTest = new ByNameSingleExtractor("age");42 // WHEN43 Object result = underTest.apply(YODA);44 // THEN45 then(result).isEqualTo(800);46 }47 @Test48 void should_extract_pure_property_values() {49 // GIVEN50 ByNameSingleExtractor underTest = new ByNameSingleExtractor("adult");51 // WHEN52 Object result = underTest.apply(YODA);53 // THEN54 then(result).isEqualTo(true);55 }56 @Test57 void should_throw_error_when_no_property_nor_public_field_match_given_name() {58 // GIVEN59 ByNameSingleExtractor underTest = new ByNameSingleExtractor("unknown");60 // WHEN61 Throwable thrown = catchThrowable(() -> underTest.apply(YODA));62 // THEN63 then(thrown).isInstanceOf(IntrospectionError.class);64 }65 @Test66 void should_throw_exception_when_given_name_is_null() {67 // GIVEN68 ByNameSingleExtractor underTest = new ByNameSingleExtractor(null);69 // WHEN70 Throwable thrown = catchThrowable(() -> underTest.apply(YODA));71 // THEN72 then(thrown).isInstanceOf(IllegalArgumentException.class)73 .hasMessage("The name of the property/field to read should not be null");74 }75 @Test76 void should_throw_exception_when_given_name_is_empty() {77 // GIVEN78 ByNameSingleExtractor underTest = new ByNameSingleExtractor("");79 // WHEN80 Throwable thrown = catchThrowable(() -> underTest.apply(YODA));81 // THEN82 then(thrown).isInstanceOf(IllegalArgumentException.class)83 .hasMessage("The name of the property/field to read should not be empty");84 }85 @Test86 void should_fallback_to_field_if_exception_has_been_thrown_on_property_access() {87 // GIVEN88 Employee employee = new Employee(1L, new Name("Name"), 0) {89 @Override90 public Name getName() {91 throw new RuntimeException();92 }93 };94 ByNameSingleExtractor underTest = new ByNameSingleExtractor("name");95 // WHEN96 Object result = underTest.apply(employee);97 // THEN98 then(result).isEqualTo(new Name("Name"));99 }100 @Test101 void should_prefer_properties_over_fields() {102 // GIVEN103 Employee employee = new Employee(1L, new Name("Name"), 0) {104 @Override105 public Name getName() {106 return new Name("Overridden Name");107 }108 };109 ByNameSingleExtractor underTest = new ByNameSingleExtractor("name");110 // WHEN111 Object result = underTest.apply(employee);112 // THEN113 then(result).isEqualTo(new Name("Overridden Name"));114 }115 @Test116 void should_throw_exception_if_property_cannot_be_extracted_due_to_runtime_exception_during_property_access() {117 // GIVEN118 Employee employee = new Employee() {119 @Override120 public boolean isAdult() {121 throw new RuntimeException();122 }123 };124 ByNameSingleExtractor underTest = new ByNameSingleExtractor("adult");125 // WHEN126 Throwable thrown = catchThrowable(() -> underTest.apply(employee));127 // THEN128 then(thrown).isInstanceOf(IntrospectionError.class);129 }130 @Test131 void should_throw_exception_if_no_object_is_given() {132 // GIVEN133 ByNameSingleExtractor underTest = new ByNameSingleExtractor("id");134 // WHEN135 Throwable thrown = catchThrowable(() -> underTest.apply(null));136 // THEN137 then(thrown).isInstanceOf(IllegalArgumentException.class);138 }139 @Test140 void should_extract_single_value_from_map_by_key() {141 // GIVEN142 Map<String, Employee> map = mapOf(entry("key", YODA));143 ByNameSingleExtractor underTest = new ByNameSingleExtractor("key");144 // WHEN145 Object result = underTest.apply(map);146 // THEN147 then(result).isEqualTo(YODA);148 }149 @Test150 void should_throw_error_from_map_by_non_existing_key() {151 // GIVEN152 Map<String, Employee> map = mapOf(entry("key", YODA));153 ByNameSingleExtractor underTest = new ByNameSingleExtractor("non-existing");154 // WHEN155 Throwable thrown = catchThrowable(() -> underTest.apply(map));156 // THEN157 then(thrown).isInstanceOf(IntrospectionError.class);158 }159 @Test160 void should_extract_null_from_map_by_key_with_null_value() {161 // GIVEN162 Map<String, Employee> map = mapOf(entry("key", null));163 ByNameSingleExtractor underTest = new ByNameSingleExtractor("key");164 // WHEN165 Object result = underTest.apply(map);166 // THEN167 then(result).isNull();168 }169 @Test170 void should_extract_property_field_combinations() {171 // GIVEN172 Employee darth = new Employee(1L, new Name("Darth", "Vader"), 100);173 Employee luke = new Employee(2L, new Name("Luke", "Skywalker"), 26);174 darth.field = luke;175 luke.field = darth;176 luke.surname = new Name("Young", "Padawan");177 ByNameSingleExtractor underTest = new ByNameSingleExtractor("me.field.me.field.me.field.surname.name");178 // WHEN179 Object result = underTest.apply(darth);180 // THEN181 then(result).isEqualTo("Young Padawan");182 }183 @Test184 void should_extract_property_with_bare_name_method() {185 // GIVEN186 BareOptionalIntHolder holder = new BareOptionalIntHolder(42);187 ByNameSingleExtractor underTest = new ByNameSingleExtractor("value");188 // WHEN189 Object result = underTest.apply(holder);190 // THEN191 then(result).isEqualTo(OptionalInt.of(42));192 }193 @Test194 void should_ignore_property_with_bare_name_method_when_disabled() {195 try {196 // GIVEN197 Introspection.setExtractBareNamePropertyMethods(false);198 BareOptionalIntHolder holder = new BareOptionalIntHolder(42);199 ByNameSingleExtractor underTest = new ByNameSingleExtractor("value");200 // WHEN201 Object result = underTest.apply(holder);202 // THEN203 then(result).isEqualTo(42);204 } finally {205 Introspection.setExtractBareNamePropertyMethods(true);206 }207 }208 /** This style of Optional handling is emitted by Immutables code gen library. */209 static class BareOptionalIntHolder {210 private final Integer value;211 BareOptionalIntHolder() {212 value = null;213 }...

Full Screen

Full Screen

ByNameSingleExtractor

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat; 2import static org.assertj.core.api.Assertions.entry; 3import static org.assertj.core.extractor.Extractors.byName; 4import static org.assertj.core.extractor.Extractors.byNameSingle; 5import static org.assertj.core.util.Lists.list; 6import java.util.List; 7import org.assertj.core.api.MapAssert; 8import org.assertj.core.api.MapAssertBaseTest; 9import org.assertj.core.data.MapEntry; 10import org.assertj.core.test.Maps; 11import org.junit.Test; 12public class MapAssert_extracting_byNameSingle_Test extends MapAssertBaseTest { 13 public void should_allow_assertions_on_property_extracted_from_given_maps_by_name() { 14 maps.put("name", Maps.mapOf(entry("first", "Yoda"), entry("last", "Solo"))); 15 maps.put("color", Maps.mapOf(entry("first", "green"), entry("last", "red"))); 16 maps.put("age", Maps.mapOf(entry("first", 800), entry("last", 100))); 17 assertThat(maps).extracting(byNameSingle("name")) 18 .containsOnly(Maps.mapOf(entry("first", "Yoda"), entry("last", "Solo"))); 19 assertThat(maps).extracting(byNameSingle("color")) 20 .containsOnly(Maps.mapOf(entry("first", "green"), entry("last", "red"))); 21 assertThat(maps).extracting(byNameSingle("age")) 22 .containsOnly(Maps.mapOf(entry("first", 800), entry("last", 100))); 23 } 24 public void should_allow_assertions_on_property_extracted_from_given_maps_by_name_with_multiple_entries() { 25 maps.put("name", Maps.mapOf(entry("first", "Yoda"), entry("last", "Solo"))); 26 maps.put("color", Maps.mapOf(entry("first", "green"), entry("last", "red"))); 27 maps.put("age", Maps.mapOf(entry("first", 800), entry("last", 100))); 28 assertThat(maps).extracting("name", "age") 29 .containsOnly(tuple(Maps.mapOf(entry("first", "Yoda"), entry("last", "Solo")), 30 Maps.mapOf(entry("first", 800), entry("last", 100)))); 31 }

Full Screen

Full Screen

ByNameSingleExtractor

Using AI Code Generation

copy

Full Screen

1org.assertj.core.extractor.Extractors.byNameSingleExtractor("name");2org.assertj.core.extractor.Extractors.byNameMultipleExtractor("name");3org.assertj.core.extractor.Extractors.byNameSingleExtractor("name");4org.assertj.core.extractor.Extractors.byNameMultipleExtractor("name");5org.assertj.core.extractor.Extractors.byNameSingleExtractor("name");6org.assertj.core.extractor.Extractors.byNameMultipleExtractor("name");7org.assertj.core.extractor.Extractors.byNameSingleExtractor("name");8org.assertj.core.extractor.Extractors.byNameMultipleExtractor("name");9org.assertj.core.extractor.Extractors.byNameSingleExtractor("name");10org.assertj.core.extractor.Extractors.byNameMultipleExtractor("name");11org.assertj.core.extractor.Extractors.byNameSingleExtractor("name");12org.assertj.core.extractor.Extractors.byNameMultipleExtractor("name");13org.assertj.core.extractor.Extractors.byNameSingleExtractor("name");14org.assertj.core.extractor.Extractors.byNameMultipleExtractor("name");15org.assertj.core.extractor.Extractors.byNameSingleExtractor("name");16org.assertj.core.extractor.Extractors.byNameMultipleExtractor("name");

Full Screen

Full Screen

ByNameSingleExtractor

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.extractor.*;2import org.assertj.core.api.*;3import org.assertj.core.api.Assertions.*;4import org.assertj.core.util.*;5import org.assertj.core.data.*;6import org.assertj.co

Full Screen

Full Screen

ByNameSingleExtractor

Using AI Code Generation

copy

Full Screen

1List<Person> persons = new ArrayList<Person>();2persons.add(new Person("John", "Doe"));3persons.add(new Person("Jane", "Doe"));4String firstName = extract(persons).with(new ByNameSingleExtractor<Person>("firstName")).asString();5assertThat(firstName).isEqualTo("John");6List<Person> persons = new ArrayList<Person>();7persons.add(new Person("John", "Doe"));8persons.add(new Person("Jane", "Doe"));9List<String> firstNames = extract(persons).with(new ByNameMultipleExtractor<Person>("firstName")).asList();10assertThat(firstNames).contains("John","Jane");11List<Person> persons = new ArrayList<Person>();12persons.add(new Person("John", "Doe"));13persons.add(new Person("Jane", "Doe"));14String firstName = extract(persons).with("firstName").asString();15assertThat(firstName).isEqualTo("John");16List<Person> persons = new ArrayList<Person>();17persons.add(new Person("John", "Doe"));18persons.add(new Person("Jane", "Doe"));19List<String> firstNames = extract(persons).with("firstName").asList();20assertThat(firstNames).contains("John","Jane");21List<Person> persons = new ArrayList<Person>();22persons.add(new Person("John", "Doe"));23persons.add(new Person("Jane", "Doe"));24String firstName = extract(persons).with("firstName", String.class).asString();25assertThat(firstName).isEqualTo("John");26List<Person> persons = new ArrayList<Person>();27persons.add(new Person("John", "Doe"));28persons.add(new Person("Jane", "Doe"));

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 ByNameSingleExtractor

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