Best Assertj code snippet using org.assertj.core.test.Name.equals
Source:GenreServiceTest.java
1package com.boivalenko.businessapp.web.app.spring.service;2import com.boivalenko.businessapp.web.app.spring.entities.Book;3import com.boivalenko.businessapp.web.app.spring.entities.Genre;4import com.boivalenko.businessapp.web.app.spring.repository.GenreRepository;5import org.junit.jupiter.api.Assertions;6import org.junit.jupiter.api.Test;7import org.junit.jupiter.api.extension.ExtendWith;8import org.mockito.InjectMocks;9import org.mockito.Mock;10import org.mockito.junit.jupiter.MockitoExtension;11import org.springframework.http.HttpStatus;12import org.springframework.http.ResponseEntity;13import java.util.*;14import static org.mockito.Mockito.*;15@ExtendWith(MockitoExtension.class)16class GenreServiceTest {17 @InjectMocks18 private GenreService genreService;19 @Mock20 private GenreRepository mockedGenreRepository;21 private final String GENRE_DUMMY_1 = "genreDummy";22 private final String GENRE_DUMMY_2 = "geNREDummy2g";23 private final String GENRE_DUMMY_3 = "_dsgenreDUMMy3";24 private final String GENRE_DUMMY_4 = "df_genrEDummy4";25 //findGenresByNameContainingIgnoreCaseOrderByNameAsc26 // Find Genre by Name Containing Ignore Case Order By Name Asc27 ////////**** Negative Tests ****//////////28 @Test29 void find_null_name() {30 String name = null;31 when(this.mockedGenreRepository.findGenresByNameContainingIgnoreCaseOrderByNameAsc(name)).thenReturn(Collections.EMPTY_LIST);32 ResponseEntity<List<Genre>> genres = this.genreService.findGenresByNameContainingIgnoreCaseOrderByNameAsc(name);33 Assertions.assertEquals(GenreService.GAR_KEIN_GENRE_VORHANDEN, genres.getBody());34 verify(mockedGenreRepository, times(1)).findGenresByNameContainingIgnoreCaseOrderByNameAsc(name);35 org.assertj.core.api.Assertions.assertThatNoException();36 }37 @Test38 void find_name_mit_bezeichnung_null() {39 String name = "null";40 when(this.mockedGenreRepository.findGenresByNameContainingIgnoreCaseOrderByNameAsc(name)).thenReturn(Collections.EMPTY_LIST);41 ResponseEntity<List<Genre>> genres = this.genreService.findGenresByNameContainingIgnoreCaseOrderByNameAsc(name);42 verify(mockedGenreRepository, times(1)).findGenresByNameContainingIgnoreCaseOrderByNameAsc(name);43 Assertions.assertEquals(GenreService.GAR_KEIN_GENRE_VORHANDEN, genres.getBody());44 org.assertj.core.api.Assertions.assertThatNoException();45 }46 @Test47 void find_nicht_existierenden_Name() {48 String name = UUID.randomUUID().toString().replaceAll("_", "")49 + UUID.randomUUID().toString().replaceAll("_", "")50 + UUID.randomUUID().toString().replaceAll("_", "");51 when(this.mockedGenreRepository.findGenresByNameContainingIgnoreCaseOrderByNameAsc(name)).thenReturn(Collections.EMPTY_LIST);52 ResponseEntity<List<Genre>> genres = this.genreService.findGenresByNameContainingIgnoreCaseOrderByNameAsc(name);53 Assertions.assertEquals(GenreService.GAR_KEIN_GENRE_VORHANDEN, genres.getBody());54 verify(mockedGenreRepository, times(1)).findGenresByNameContainingIgnoreCaseOrderByNameAsc(name);55 org.assertj.core.api.Assertions.assertThatNoException();56 }57 @Test58 void find_zu_langen_Name() {59 String name = "zuLANGEEE" + UUID.randomUUID().toString().replaceAll("_", "")60 + UUID.randomUUID().toString().replaceAll("_", "")61 + UUID.randomUUID().toString().replaceAll("_", "");62 when(this.mockedGenreRepository.findGenresByNameContainingIgnoreCaseOrderByNameAsc(name)).thenReturn(Collections.EMPTY_LIST);63 ResponseEntity<List<Genre>> genres = this.genreService.findGenresByNameContainingIgnoreCaseOrderByNameAsc(name);64 Assertions.assertEquals(GenreService.GAR_KEIN_GENRE_VORHANDEN, genres.getBody());65 verify(mockedGenreRepository, times(1)).findGenresByNameContainingIgnoreCaseOrderByNameAsc(name);66 org.assertj.core.api.Assertions.assertThatNoException();67 }68 ////////**** Positive Tests ****//////////69 @Test70 void find_richtige_Namen() {71 Calendar calendar = new GregorianCalendar();72 calendar.set(1988, 8, 6);73 List<Book> books = new ArrayList<>();74 books.add(new Book());75 books.add(new Book());76 books.add(new Book());77 List<Genre> genres = Arrays.asList(78 new Genre(GENRE_DUMMY_1, books),79 new Genre(GENRE_DUMMY_2, books),80 new Genre(GENRE_DUMMY_3, books),81 new Genre(GENRE_DUMMY_4, books)82 );83 when(this.mockedGenreRepository.findGenresByNameContainingIgnoreCaseOrderByNameAsc(GENRE_DUMMY_1)).thenReturn(genres);84 ResponseEntity<List<Genre>> genresResponse = this.genreService.findGenresByNameContainingIgnoreCaseOrderByNameAsc(GENRE_DUMMY_1);85 Assertions.assertFalse(genresResponse.getBody().isEmpty());86 org.assertj.core.api.Assertions.assertThat(genresResponse.getBody().size()).isGreaterThanOrEqualTo(genres.size());87 verify(mockedGenreRepository, times(1)).findGenresByNameContainingIgnoreCaseOrderByNameAsc(GENRE_DUMMY_1);88 org.assertj.core.api.Assertions.assertThatNoException();89 }90 //save91 //Negativ92 @Test93 void save_NullGenre() {94 Genre genre = null;95 ResponseEntity<Genre> genres = this.genreService.save(genre);96 Assertions.assertEquals(GenreService.GENRE_DARF_NICHT_NULL_SEIN, genres.getBody());97 verify(mockedGenreRepository, times(0)).save(genre);98 org.assertj.core.api.Assertions.assertThatNoException();99 }100 @Test101 void save_genreId_eingeben() {102 Genre genre = new Genre();103 genre.setId(1L);104 ResponseEntity<Genre> genres = this.genreService.save(genre);105 Assertions.assertEquals(GenreService.ID_WIRD_AUTOMATISCH_GENERIERT_MAN_MUSS_DAS_NICHT_EINGEBEN, genres.getBody());106 verify(mockedGenreRepository, times(0)).save(genre);107 org.assertj.core.api.Assertions.assertThatNoException();108 }109 @Test110 void save_nameNull_eingeben() {111 Genre genre = new Genre();112 genre.setName(null);113 ResponseEntity<Genre> genres = this.genreService.save(genre);114 Assertions.assertEquals(GenreService.NAME_DARF_WEDER_NULL_NOCH_LEER_SEIN, genres.getBody());115 verify(mockedGenreRepository, times(0)).save(genre);116 org.assertj.core.api.Assertions.assertThatNoException();117 }118 @Test119 void save_nameContainNull_eingeben() {120 Genre genre = new Genre();121 genre.setName("BlanUlLBlaB");122 ResponseEntity<Genre> genres = this.genreService.save(genre);123 Assertions.assertEquals(GenreService.NAME_DARF_WEDER_NULL_NOCH_LEER_SEIN, genres.getBody());124 verify(mockedGenreRepository, times(0)).save(genre);125 org.assertj.core.api.Assertions.assertThatNoException();126 }127 @Test128 void save_nameLeer_eingeben() {129 Genre genre = new Genre();130 genre.setName("");131 ResponseEntity<Genre> genres = this.genreService.save(genre);132 Assertions.assertEquals(GenreService.NAME_DARF_WEDER_NULL_NOCH_LEER_SEIN, genres.getBody());133 verify(mockedGenreRepository, times(0)).save(genre);134 org.assertj.core.api.Assertions.assertThatNoException();135 }136 //Positiv137 @Test138 void save() {139 Genre genre = new Genre();140 genre.setName("GanzNormalerName");141 ResponseEntity<Genre> genres = this.genreService.save(genre);142 Assertions.assertEquals(HttpStatus.OK, genres.getStatusCode());143 verify(mockedGenreRepository, times(1)).save(genre);144 org.assertj.core.api.Assertions.assertThatNoException();145 }146 //update147 //Negativ148 @Test149 void update_NullGenre() {150 Genre genre = null;151 ResponseEntity<Genre> genres = this.genreService.update(genre);152 Assertions.assertEquals(GenreService.GENRE_DARF_NICHT_NULL_SEIN, genres.getBody());153 verify(mockedGenreRepository, times(0)).save(genre);154 org.assertj.core.api.Assertions.assertThatNoException();155 }156 @Test157 void update_genreIdAls0_eingeben() {158 Genre genre = new Genre();159 genre.setId(0L);160 ResponseEntity<Genre> genres = this.genreService.update(genre);161 Assertions.assertEquals(GenreService.ID_DARF_WEDER_NULL_NOCH_0_SEIN, genres.getBody());162 verify(mockedGenreRepository, times(0)).save(genre);163 org.assertj.core.api.Assertions.assertThatNoException();164 }165 @Test166 void update_genreIdAlsNull_eingeben() {167 Genre genre = new Genre();168 genre.setId(null);169 ResponseEntity<Genre> genres = this.genreService.update(genre);170 Assertions.assertEquals(GenreService.ID_DARF_WEDER_NULL_NOCH_0_SEIN, genres.getBody());171 verify(mockedGenreRepository, times(0)).save(genre);172 org.assertj.core.api.Assertions.assertThatNoException();173 }174 @Test175 void update_nameNull_eingeben() {176 Genre genre = new Genre();177 genre.setId(1L);178 genre.setName(null);179 ResponseEntity<Genre> genres = this.genreService.update(genre);180 Assertions.assertEquals(GenreService.NAME_DARF_WEDER_NULL_NOCH_LEER_SEIN, genres.getBody());181 verify(mockedGenreRepository, times(0)).save(genre);182 org.assertj.core.api.Assertions.assertThatNoException();183 }184 @Test185 void update_nameContainNull_eingeben() {186 Genre genre = new Genre();187 genre.setId(1L);188 genre.setName("BlanUlLBlaB");189 ResponseEntity<Genre> genres = this.genreService.update(genre);190 Assertions.assertEquals(GenreService.NAME_DARF_WEDER_NULL_NOCH_LEER_SEIN, genres.getBody());191 verify(mockedGenreRepository, times(0)).save(genre);192 org.assertj.core.api.Assertions.assertThatNoException();193 }194 @Test195 void update_nameLeer_eingeben() {196 Genre genre = new Genre();197 genre.setId(1L);198 genre.setName("");199 ResponseEntity<Genre> genres = this.genreService.update(genre);200 Assertions.assertEquals(GenreService.NAME_DARF_WEDER_NULL_NOCH_LEER_SEIN, genres.getBody());201 verify(mockedGenreRepository, times(0)).save(genre);202 org.assertj.core.api.Assertions.assertThatNoException();203 }204 @Test205 void update_nameID_exist_nicht() {206 Genre genre = new Genre();207 Long id = 1L;208 genre.setId(id);209 genre.setName("GanzNormalerName");210 when(this.mockedGenreRepository.existsById(id)).thenReturn(false);211 ResponseEntity<Genre> genres = this.genreService.update(genre);212 Assertions.assertEquals("ID=" + genre.getId() + " nicht gefunden", genres.getBody());213 verify(mockedGenreRepository, times(0)).save(genre);214 org.assertj.core.api.Assertions.assertThatNoException();215 }216 //Positiv217 @Test218 void update() {219 Genre genre = new Genre();220 Long id = 1L;221 genre.setId(id);222 genre.setName("GanzNormalerName");223 when(this.mockedGenreRepository.existsById(id)).thenReturn(true);224 ResponseEntity<Genre> genres = this.genreService.update(genre);225 Assertions.assertEquals(HttpStatus.OK, genres.getStatusCode());226 verify(mockedGenreRepository, times(1)).save(genre);227 org.assertj.core.api.Assertions.assertThatNoException();228 }229 //deleteById230 //Negativ231 @Test232 void delete_genreIdAlsNull_eingeben() {233 Genre genre = new Genre();234 Long id = null;235 genre.setId(id);236 ResponseEntity<Genre> genres = this.genreService.deleteById(id);237 Assertions.assertEquals(GenreService.ID_DARF_WEDER_NULL_NOCH_0_SEIN, genres.getBody());238 verify(mockedGenreRepository, times(0)).deleteById(id);239 org.assertj.core.api.Assertions.assertThatNoException();240 }241 @Test242 void delete_genreIdAls0_eingeben() {243 Long id = 0L;244 ResponseEntity<Genre> genres = this.genreService.deleteById(id);245 Assertions.assertEquals(GenreService.ID_DARF_WEDER_NULL_NOCH_0_SEIN, genres.getBody());246 verify(mockedGenreRepository, times(0)).deleteById(id);247 org.assertj.core.api.Assertions.assertThatNoException();248 }249 @Test250 void delete_nameID_exist_nicht() {251 Genre genre = new Genre();252 Long id = 1L;253 genre.setId(id);254 genre.setName("GanzNormalerName");255 when(this.mockedGenreRepository.existsById(id)).thenReturn(false);256 ResponseEntity<Genre> genres = this.genreService.deleteById(id);257 Assertions.assertEquals("ID=" + genre.getId() + " nicht gefunden", genres.getBody());258 verify(mockedGenreRepository, times(0)).deleteById(id);259 org.assertj.core.api.Assertions.assertThatNoException();260 }261 //Positiv262 @Test263 void deleteById() {264 Genre genre = new Genre();265 Long id = 1L;266 genre.setId(id);267 genre.setName("GanzNormalerName");268 when(this.mockedGenreRepository.existsById(id)).thenReturn(true);269 ResponseEntity<Genre> genres = this.genreService.deleteById(id);270 Assertions.assertEquals(HttpStatus.OK, genres.getStatusCode());271 verify(mockedGenreRepository, times(1)).deleteById(id);272 org.assertj.core.api.Assertions.assertThatNoException();273 }274 //findById275 //Negativ276 @Test277 void findById_genreIdAlsNull_eingeben() {278 Genre genre = new Genre();279 Long id = null;280 genre.setId(id);281 ResponseEntity<Genre> genres = this.genreService.findById(id);282 Assertions.assertEquals(GenreService.ID_DARF_WEDER_NULL_NOCH_0_SEIN, genres.getBody());283 verify(mockedGenreRepository, times(0)).findById(id);284 org.assertj.core.api.Assertions.assertThatNoException();285 }286 @Test287 void findById_genreIdAls0_eingeben() {288 Long id = 0L;289 ResponseEntity<Genre> genres = this.genreService.findById(id);290 Assertions.assertEquals(GenreService.ID_DARF_WEDER_NULL_NOCH_0_SEIN, genres.getBody());291 verify(mockedGenreRepository, times(0)).findById(id);292 org.assertj.core.api.Assertions.assertThatNoException();293 }294 @Test295 void findById_nameID_exist_nicht() {296 Genre genre = new Genre();297 Long id = 1L;298 genre.setId(id);299 genre.setName("GanzNormalerName");300 when(this.mockedGenreRepository.existsById(id)).thenReturn(false);301 ResponseEntity<Genre> genres = this.genreService.findById(id);302 Assertions.assertEquals("ID=" + genre.getId() + " nicht gefunden", genres.getBody());303 verify(mockedGenreRepository, times(0)).findById(id);304 org.assertj.core.api.Assertions.assertThatNoException();305 }306 //Positiv307 @Test308 void findById() {309 Genre genre = new Genre();310 Long id = 1L;311 genre.setId(id);312 genre.setName("GanzNormalerName");313 Optional<Genre> genreOptional = Optional.of(genre);314 when(this.mockedGenreRepository.existsById(id)).thenReturn(true);315 doReturn(genreOptional).when(this.mockedGenreRepository).findById(id);316 ResponseEntity<Genre> genres = this.genreService.findById(id);317 Assertions.assertEquals(HttpStatus.OK, genres.getStatusCode());318 verify(mockedGenreRepository, times(1)).findById(id);319 org.assertj.core.api.Assertions.assertThatNoException();320 }321 //findAll322 //Negativ323 @Test324 void findAll_genres_null() {325 List<Genre> genres = null;326 when(this.mockedGenreRepository.findAll()).thenReturn(genres);327 ResponseEntity<List<Genre>> all = this.genreService.findAll();328 Assertions.assertEquals(GenreService.GAR_KEIN_GENRE_VORHANDEN, all.getBody());329 verify(mockedGenreRepository, times(1)).findAll();330 org.assertj.core.api.Assertions.assertThatNoException();331 }332 @Test333 void findAll_genres_empty() {334 List<Genre> genres = Collections.EMPTY_LIST;335 when(this.mockedGenreRepository.findAll()).thenReturn(genres);336 ResponseEntity<List<Genre>> all = this.genreService.findAll();337 Assertions.assertEquals(GenreService.GAR_KEIN_GENRE_VORHANDEN, all.getBody());338 verify(mockedGenreRepository, times(1)).findAll();339 org.assertj.core.api.Assertions.assertThatNoException();340 }341 //Positiv342 @Test343 void findAll() {344 List<Genre> genres = new ArrayList<>();345 genres.add(new Genre());346 when(this.mockedGenreRepository.findAll()).thenReturn(genres);347 ResponseEntity<List<Genre>> all = this.genreService.findAll();348 Assertions.assertEquals(HttpStatus.OK, all.getStatusCode());349 verify(mockedGenreRepository, times(1)).findAll();350 org.assertj.core.api.Assertions.assertThatNoException();351 }352}...
Source:ListAssert_assertionState_propagation_with_extracting_Test.java
1/*2 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with3 * the License. You may obtain a copy of the License at4 *5 * http://www.apache.org/licenses/LICENSE-2.06 *7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on8 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the9 * specific language governing permissions and limitations under the License.10 *11 * Copyright 2012-2022 the original author or authors.12 */13package org.assertj.core.api.list;14import static org.assertj.core.api.Assertions.assertThat;15import static org.assertj.core.api.Assertions.tuple;16import static org.assertj.core.api.GroupAssertTestHelper.comparatorForElementFieldsWithNamesOf;17import static org.assertj.core.api.GroupAssertTestHelper.comparatorForElementFieldsWithTypeOf;18import static org.assertj.core.api.GroupAssertTestHelper.comparatorsByTypeOf;19import static org.assertj.core.api.GroupAssertTestHelper.firstNameFunction;20import static org.assertj.core.api.GroupAssertTestHelper.lastNameFunction;21import static org.assertj.core.api.GroupAssertTestHelper.throwingFirstNameExtractor;22import static org.assertj.core.extractor.Extractors.byName;23import static org.assertj.core.presentation.UnicodeRepresentation.UNICODE_REPRESENTATION;24import static org.assertj.core.test.AlwaysEqualComparator.ALWAYS_EQUALS_STRING;25import static org.assertj.core.test.AlwaysEqualComparator.ALWAYS_EQUALS_TIMESTAMP;26import static org.assertj.core.test.AlwaysEqualComparator.ALWAYS_EQUALS_TUPLE;27import static org.assertj.core.util.Lists.newArrayList;28import java.sql.Timestamp;29import java.util.List;30import org.assertj.core.api.AbstractListAssert;31import org.assertj.core.groups.Tuple;32import org.assertj.core.test.Employee;33import org.assertj.core.test.Name;34import org.junit.jupiter.api.BeforeEach;35import org.junit.jupiter.api.Test;36class ListAssert_assertionState_propagation_with_extracting_Test {37 private Employee yoda;38 private Employee luke;39 private List<Employee> jedis;40 @BeforeEach41 void setUp() {42 yoda = new Employee(1L, new Name("Yoda"), 800);43 luke = new Employee(2L, new Name("Luke", "Skywalker"), 26);44 jedis = newArrayList(yoda, luke);45 }46 @Test47 void extracting_by_several_functions_should_keep_assertion_state() {48 // WHEN49 // not all comparators are used but we want to test that they are passed correctly after extracting50 AbstractListAssert<?, ?, ?, ?> assertion = assertThat(jedis).as("test description")51 .withFailMessage("error message")52 .withRepresentation(UNICODE_REPRESENTATION)53 .usingComparatorForElementFieldsWithNames(ALWAYS_EQUALS_STRING,54 "foo")55 .usingComparatorForElementFieldsWithType(ALWAYS_EQUALS_TIMESTAMP,56 Timestamp.class)57 .usingComparatorForType(ALWAYS_EQUALS_TUPLE, Tuple.class)58 .extracting(firstNameFunction, lastNameFunction)59 .contains(tuple("YODA", null), tuple("Luke", "Skywalker"));60 // THEN61 assertThat(assertion.descriptionText()).isEqualTo("test description");62 assertThat(assertion.info.representation()).isEqualTo(UNICODE_REPRESENTATION);63 assertThat(assertion.info.overridingErrorMessage()).isEqualTo("error message");64 assertThat(comparatorsByTypeOf(assertion).getComparatorForType(Tuple.class)).isSameAs(ALWAYS_EQUALS_TUPLE);65 assertThat(comparatorForElementFieldsWithTypeOf(assertion).getComparatorForType(Timestamp.class)).isSameAs(ALWAYS_EQUALS_TIMESTAMP);66 assertThat(comparatorForElementFieldsWithNamesOf(assertion).get("foo")).isSameAs(ALWAYS_EQUALS_STRING);67 }68 @Test69 void extracting_by_name_should_keep_assertion_state() {70 // WHEN71 // not all comparators are used but we want to test that they are passed correctly after extracting72 AbstractListAssert<?, ?, ?, ?> assertion = assertThat(jedis).as("test description")73 .withFailMessage("error message")74 .withRepresentation(UNICODE_REPRESENTATION)75 .usingComparatorForElementFieldsWithNames(ALWAYS_EQUALS_STRING,76 "foo")77 .usingComparatorForElementFieldsWithType(ALWAYS_EQUALS_TIMESTAMP,78 Timestamp.class)79 .usingComparatorForType(ALWAYS_EQUALS_STRING, String.class)80 .extracting("name.first")81 .contains("YODA", "Luke");82 // THEN83 assertThat(assertion.descriptionText()).isEqualTo("test description");84 assertThat(assertion.info.representation()).isEqualTo(UNICODE_REPRESENTATION);85 assertThat(assertion.info.overridingErrorMessage()).isEqualTo("error message");86 assertThat(comparatorsByTypeOf(assertion).getComparatorForType(String.class)).isSameAs(ALWAYS_EQUALS_STRING);87 assertThat(comparatorForElementFieldsWithTypeOf(assertion).getComparatorForType(Timestamp.class)).isSameAs(ALWAYS_EQUALS_TIMESTAMP);88 assertThat(comparatorForElementFieldsWithNamesOf(assertion).get("foo")).isSameAs(ALWAYS_EQUALS_STRING);89 }90 @Test91 void extracting_by_strongly_typed_name_should_keep_assertion_state() {92 // WHEN93 // not all comparators are used but we want to test that they are passed correctly after extracting94 AbstractListAssert<?, ?, ?, ?> assertion = assertThat(jedis).as("test description")95 .withFailMessage("error message")96 .withRepresentation(UNICODE_REPRESENTATION)97 .usingComparatorForElementFieldsWithNames(ALWAYS_EQUALS_STRING,98 "foo")99 .usingComparatorForElementFieldsWithType(ALWAYS_EQUALS_TIMESTAMP,100 Timestamp.class)101 .usingComparatorForType(ALWAYS_EQUALS_STRING, String.class)102 .extracting("name.first", String.class)103 .contains("YODA", "Luke");104 // THEN105 assertThat(assertion.descriptionText()).isEqualTo("test description");106 assertThat(assertion.info.representation()).isEqualTo(UNICODE_REPRESENTATION);107 assertThat(assertion.info.overridingErrorMessage()).isEqualTo("error message");108 assertThat(comparatorsByTypeOf(assertion).getComparatorForType(String.class)).isSameAs(ALWAYS_EQUALS_STRING);109 assertThat(comparatorForElementFieldsWithTypeOf(assertion).getComparatorForType(Timestamp.class)).isSameAs(ALWAYS_EQUALS_TIMESTAMP);110 assertThat(comparatorForElementFieldsWithNamesOf(assertion).get("foo")).isSameAs(ALWAYS_EQUALS_STRING);111 }112 @Test113 void extracting_by_multiple_names_should_keep_assertion_state() {114 // WHEN115 // not all comparators are used but we want to test that they are passed correctly after extracting116 AbstractListAssert<?, ?, ?, ?> assertion = assertThat(jedis).as("test description")117 .withFailMessage("error message")118 .withRepresentation(UNICODE_REPRESENTATION)119 .usingComparatorForElementFieldsWithNames(ALWAYS_EQUALS_STRING,120 "foo")121 .usingComparatorForElementFieldsWithType(ALWAYS_EQUALS_TIMESTAMP,122 Timestamp.class)123 .usingComparatorForType(ALWAYS_EQUALS_TUPLE, Tuple.class)124 .extracting("name.first", "name.last")125 .contains(tuple("YODA", null), tuple("Luke", "Skywalker"));126 // THEN127 assertThat(assertion.descriptionText()).isEqualTo("test description");128 assertThat(assertion.info.representation()).isEqualTo(UNICODE_REPRESENTATION);129 assertThat(assertion.info.overridingErrorMessage()).isEqualTo("error message");130 assertThat(comparatorsByTypeOf(assertion).getComparatorForType(Tuple.class)).isSameAs(ALWAYS_EQUALS_TUPLE);131 assertThat(comparatorForElementFieldsWithTypeOf(assertion).getComparatorForType(Timestamp.class)).isSameAs(ALWAYS_EQUALS_TIMESTAMP);132 assertThat(comparatorForElementFieldsWithNamesOf(assertion).get("foo")).isSameAs(ALWAYS_EQUALS_STRING);133 }134 @Test135 void extracting_by_single_extractor_should_keep_assertion_state() {136 // WHEN137 // not all comparators are used but we want to test that they are passed correctly after extracting138 AbstractListAssert<?, ?, ?, ?> assertion = assertThat(jedis).as("test description")139 .withFailMessage("error message")140 .withRepresentation(UNICODE_REPRESENTATION)141 .usingComparatorForElementFieldsWithNames(ALWAYS_EQUALS_STRING,142 "foo")143 .usingComparatorForElementFieldsWithType(ALWAYS_EQUALS_TIMESTAMP,144 Timestamp.class)145 .usingComparatorForType(ALWAYS_EQUALS_STRING, String.class)146 .extracting(byName("name.first"))147 .contains("YODA", "Luke");148 // THEN149 assertThat(assertion.descriptionText()).isEqualTo("test description");150 assertThat(assertion.info.representation()).isEqualTo(UNICODE_REPRESENTATION);151 assertThat(assertion.info.overridingErrorMessage()).isEqualTo("error message");152 assertThat(comparatorsByTypeOf(assertion).getComparatorForType(String.class)).isSameAs(ALWAYS_EQUALS_STRING);153 assertThat(comparatorForElementFieldsWithTypeOf(assertion).getComparatorForType(Timestamp.class)).isSameAs(ALWAYS_EQUALS_TIMESTAMP);154 assertThat(comparatorForElementFieldsWithNamesOf(assertion).get("foo")).isSameAs(ALWAYS_EQUALS_STRING);155 }156 @Test157 void extracting_by_throwing_extractor_should_keep_assertion_state() {158 // WHEN159 // not all comparators are used but we want to test that they are passed correctly after extracting160 AbstractListAssert<?, ?, ?, ?> assertion = assertThat(jedis).as("test description")161 .withFailMessage("error message")162 .withRepresentation(UNICODE_REPRESENTATION)163 .usingComparatorForElementFieldsWithNames(ALWAYS_EQUALS_STRING,164 "foo")165 .usingComparatorForElementFieldsWithType(ALWAYS_EQUALS_TIMESTAMP,166 Timestamp.class)167 .usingComparatorForType(ALWAYS_EQUALS_STRING, String.class)168 .extracting(throwingFirstNameExtractor)169 .contains("YODA", "Luke");170 // THEN171 assertThat(assertion.descriptionText()).isEqualTo("test description");172 assertThat(assertion.info.representation()).isEqualTo(UNICODE_REPRESENTATION);173 assertThat(assertion.info.overridingErrorMessage()).isEqualTo("error message");174 assertThat(comparatorsByTypeOf(assertion).getComparatorForType(String.class)).isSameAs(ALWAYS_EQUALS_STRING);175 assertThat(comparatorForElementFieldsWithTypeOf(assertion).getComparatorForType(Timestamp.class)).isSameAs(ALWAYS_EQUALS_TIMESTAMP);176 assertThat(comparatorForElementFieldsWithNamesOf(assertion).get("foo")).isSameAs(ALWAYS_EQUALS_STRING);177 }178}...
Source:SelenideDownloadTest.java
...71 ZipFile zf = new ZipFile(new File("src/test/resources/zip/" + zipName));72 ZipInputStream is = new ZipInputStream(Objects.requireNonNull(cl.getResourceAsStream("zip/" + zipName)));73 ZipEntry entry;74 while ((entry = is.getNextEntry()) != null) {75 if (entry.getName().equals(pdfName)) {76 try (InputStream stream = zf.getInputStream(entry)) {77 assert stream != null;78 PDF pdf = new PDF(stream);79 Assertions.assertEquals(166, pdf.numberOfPages);80 assertThat(pdf, new ContainsExactText("123"));81 }82 }83 if (entry.getName().equals(csvName)) {84 try (InputStream stream = zf.getInputStream(entry)) {85 assert stream != null;86 try (CSVReader reader = new CSVReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {87 List<String[]> content = reader.readAll();88 org.assertj.core.api.Assertions.assertThat(content).contains(89 new String[]{"Name", "Surname"},90 new String[]{"Ivan", "Ivanov"},91 new String[]{"Petr", "Petrov"}92 );93 }94 }95 }96 if (entry.getName().equals(xlsxName)) {97 try (InputStream stream = zf.getInputStream(entry)) {98 assert stream != null;99 XLS xls = new XLS(stream);100 String stringCellValue = xls.excel.getSheetAt(0).getRow(1).getCell(0).getStringCellValue();101 org.assertj.core.api.Assertions.assertThat(stringCellValue).contains("ÐÑаÑнодаÑÑкий кÑай");102 }103 }104 }105 }106 @Test107 void jsonGsonParsingTest() {108 Gson g = new Gson();109 Person person = g.fromJson(JsonToString.readJsonData("src/test/resources/json/" + jsonName), Person.class);110 org.assertj.core.api.Assertions.assertThat(person.firstName).contains("John");...
equals
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import org.assertj.core.test.Name;3public class 1 {4 public static void main(String[] args) {5 Name name1 = new Name("John", "Doe");6 Name name2 = new Name("John", "Doe");7 assertThat(name1).isEqualTo(name2);8 }9}10import static org.assertj.core.api.Assertions.assertThat;11import org.assertj.core.test.Employee;12public class 2 {13 public static void main(String[] args) {14 Employee employee1 = new Employee("John", "Doe", 1000);15 Employee employee2 = new Employee("John", "Doe", 1000);16 assertThat(employee1).isEqualTo(employee2);17 }18}19import static org.assertj.core.api.Assertions.assertThat;20import org.assertj.core.test.Employee;21public class 3 {22 public static void main(String[] args) {23 Employee employee1 = new Employee("John", "Doe", 1000);24 Employee employee2 = new Employee("John", "Doe", 1000);25 assertThat(employee1).isEqualTo(employee2);26 }27}28import static org.assertj.core.api.Assertions.assertThat;29import org.assertj.core.test.Employee;30public class 4 {31 public static void main(String[] args) {32 Employee employee1 = new Employee("John", "Doe", 1000);33 Employee employee2 = new Employee("John", "Doe", 1000);34 assertThat(employee1).isEqualTo(employee2);35 }36}37import static org.assertj.core.api.Assertions.assertThat;38import org.assertj.core.test.Employee;39public class 5 {40 public static void main(String[] args) {41 Employee employee1 = new Employee("John", "Doe", 1000);42 Employee employee2 = new Employee("John", "Doe", 1000);43 assertThat(employee1).isEqualTo(employee2);44 }45}46import static org.assertj.core.api
equals
Using AI Code Generation
1import org.assertj.core.test.Name;2import static org.assertj.core.api.Assertions.assertThat;3public class 1 {4 public static void main(String[] args) {5 Name name1 = new Name("John", "Doe");6 Name name2 = new Name("John", "Doe");7 assertThat(name1).isEqualTo(name2);8 }9}10import org.assertj.core.test.Employee;11import static org.assertj.core.api.Assertions.assertThat;12public class 2 {13 public static void main(String[] args) {14 Employee employee1 = new Employee("John", "Doe", 1000);15 Employee employee2 = new Employee("John", "Doe", 1000);16 assertThat(employee1).isEqualTo(employee2);17 }18}19import org.assertj.core.test.Employee;20import static org.assertj.core.api.Assertions.assertThat;21public class 3 {22 public static void main(String[] args) {23 Employee employee1 = new Employee("John", "Doe", 1000);24 Employee employee2 = new Employee("John", "Doe", 1000);25 assertThat(employee1).isNotEqualTo(employee2);26 }27}28import org.assertj.core.test.Employee;29import static org.assertj.core.api.Assertions.assertThat;30public class 4 {31 public static void main(String[] args) {32 Employee employee1 = new Employee("John", "Doe", 1000);33 Employee employee2 = new Employee("John", "Doe", 1000);34 assertThat(employee1).isNotSameAs(employee2);35 }36}37import org.assertj.core.test.Employee;38import static org.assertj.core.api.Assertions.assertThat;39public class 5 {40 public static void main(String[] args) {41 Employee employee1 = new Employee("John", "Doe", 1000);42 Employee employee2 = new Employee("John", "Doe", 1000);43 assertThat(employee1).isSameAs(employee2);44 }45}
equals
Using AI Code Generation
1import org.assertj.core.test.Name;2import org.assertj.core.api.Assertions;3public class 1 {4 public static void main(String[] args) {5 Name name1 = new Name("John", "Doe");6 Name name2 = new Name("John", "Doe");7 Assertions.assertThat(name1).isEqualTo(name2);8 }9}10import org.assertj.core.test.Name;11import org.assertj.core.api.Assertions;12public class 2 {13 public static void main(String[] args) {14 Name name1 = new Name("John", "Doe");15 Name name2 = new Name("John", "Doe");16 Assertions.assertThat(name1).isNotEqualTo(name2);17 }18}19import org.assertj.core.test.Name;20import org.assertj.core.api.Assertions;21public class 3 {22 public static void main(String[] args) {23 Name name1 = new Name("John", "Doe");24 Name name2 = new Name("John", "Doe");25 Assertions.assertThat(name1).isSameAs(name2);26 }27}28import org.assertj.core.test.Name;29import org.assertj.core.api.Assertions;30public class 4 {31 public static void main(String[] args) {32 Name name1 = new Name("John", "Doe");33 Name name2 = new Name("John", "Doe");34 Assertions.assertThat(name1).isNotSameAs(name2);35 }36}37import org.assertj.core.test.Name;38import org.assertj.core.api.Assertions;39public class 5 {40 public static void main(String[] args) {41 Name name1 = new Name("John", "Doe");42 Name name2 = new Name("John", "Doe");43 Assertions.assertThat(name1).isNotSameAs(name2);44 }45}46import org.assertj.core.test.Name;47import org.assertj.core.api.Assertions;48public class 6 {49 public static void main(String[] args) {
equals
Using AI Code Generation
1public class Name {2 public String first;3 public String last;4 public Name(String first, String last) {5 this.first = first;6 this.last = last;7 }8 public boolean equals(Object obj) {9 Name other = (Name) obj;10 return first.equals(other.first) && last.equals(other.last);11 }12}13public class 1 {14 public void test() {15 Name name = new Name("Yoda", "Obiwan");16 assertThat(name).isEqualTo(new Name("Yoda", "Obiwan"));17 }18}19public class Person {20 public String name;21 public int age;22 public Person(String name, int age) {23 this.name = name;24 this.age = age;25 }26 public boolean equals(Object obj) {27 Person other = (Person) obj;28 return name.equals(other.name) && age == other.age;29 }30}31public class 2 {32 public void test() {33 Person person = new Person("Yoda", 800);34 assertThat(person).isEqualTo(new Person("Yoda", 800));35 }36}37public class Employee extends Person {38 public int salary;39 public Employee(String name, int age, int salary) {40 super(name, age);41 this.salary = salary;42 }43 public boolean equals(Object obj) {44 Employee other = (Employee) obj;45 return super.equals(other) && salary == other.salary;46 }47}48public class 3 {49 public void test() {50 Employee employee = new Employee("Yoda", 800, 1000000);51 assertThat(employee).isEqualTo(new Employee("Yoda", 800, 1000000));52 }53}54public class Employee extends Person {55 public int salary;56 public Employee(String name, int age, int salary) {57 super(name, age);58 this.salary = salary;59 }60 public boolean equals(Object obj) {61 Employee other = (Employee) obj;
equals
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.within;3import static org.assertj.core.test.Name.name;4import static org.assertj.core.util.Lists.newArrayList;5import java.util.List;6import org.assertj.core.test.Name;7import org.junit.Test;8public class AssertJTest {9public void test() {10Name actual = name("John", "Doe");11assertThat(actual).isEqualTo(name("John", "Doe"));12assertThat(actual).isNotEqualTo(name("Jane", "Doe"));13assertThat(actual).isEqualToComparingFieldByField(name("John", "Doe"));14assertThat(actual).isNotEqualToComparingFieldByField(name("Jane", "Doe"));15assertThat(actual).isEqualToIgnoringGivenFields(name("John", "Doe"), "last");16assertThat(actual).isNotEqualToIgnoringGivenFields(name("Jane", "Doe"), "last");17assertThat(actual).isEqualToIgnoringNullFields(name("John", "Doe"));18assertThat(actual).isNotEqualToIgnoringNullFields(name("Jane", "Doe"));19assertThat(actual).isEqualToComparingOnlyGivenFields(name("John", "Doe"), "first");20assertThat(actual).isNotEqualToComparingOnlyGivenFields(name("Jane", "Doe"), "first");21assertThat(actual).isEqualToComparingFieldByFieldRecursively(name("John", "Doe"));22assertThat(actual).isNotEqualToComparingFieldByFieldRecursively(name("Jane", "Doe"));23assertThat(actual).isEqualToIgnoringCase(name("john", "doe"));24assertThat(actual).isNotEqualToIgnoringCase(name("jane", "doe"));25assertThat(actual).isEqualToIgnoringWhitespace(name("John", "Doe"));26assertThat(actual).isNotEqualToIgnoringWhitespace(name("Jane", "Doe"));27assertThat(actual).isIn(newArrayList(name("John", "Doe"), name("Jane", "Doe")));28assertThat(actual).isNotIn(newArrayList(name("Jane", "Doe"), name("Joe", "Doe")));
equals
Using AI Code Generation
1import org.assertj.core.test.Name;2import org.assertj.core.api.Assertions;3import org.junit.Test;4public class 1 {5 public void test() {6 Name name = new Name("Yoda");7 Assertions.assertThat(name).isEqualTo(new Name("Yoda"));8 }9}10package org.assertj.core.test;11public class Name {12 private String name;13 public Name(String name) {14 this.name = name;15 }16 public String getName() {17 return name;18 }19 public boolean equals(Object obj) {20 if (obj == null)21 return false;22 if (obj == this)23 return true;24 if (obj.getClass() != getClass())25 return false;26 Name rhs = (Name) obj;27 return name.equals(rhs.name);28 }29 public int hashCode() {30 return 31 * 7 + (name == null ? 0 : name.hashCode());31 }32}33I am using IntelliJ IDEA 2017.2.6 (Community Edition) Build #IC-172.4574.11, built on September 5, 2017 JRE: 1.8.0_152-release-915-b12 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0
equals
Using AI Code Generation
1Name name1 = new Name("John", "Doe");2Name name2 = new Name("John", "Doe");3assertThat(name1).isEqualTo(name2);4Person person1 = new Person("John", "Doe");5Person person2 = new Person("John", "Doe");6assertThat(person1).isEqualTo(person2);7String string1 = "John";8String string2 = "John";9assertThat(string1).isEqualTo(string2);10Integer integer1 = 1;11Integer integer2 = 1;12assertThat(integer1).isEqualTo(integer2);13Long long1 = 1L;14Long long2 = 1L;15assertThat(long1).isEqualTo(long2);16Short short1 = 1;17Short short2 = 1;18assertThat(short1).isEqualTo(short2);19Byte byte1 = 1;20Byte byte2 = 1;21assertThat(byte1).isEqualTo(byte2);22Double double1 = 1.0;23Double double2 = 1.0;24assertThat(double1).isEqualTo(double2);25Float float1 = 1.0f;26Float float2 = 1.0f;27assertThat(float1).isEqualTo(float2);28Boolean boolean1 = true;29Boolean boolean2 = true;30assertThat(boolean1).isEqualTo(boolean2);31Character character1 = 'a';32Character character2 = 'a';33assertThat(character1).isEqualTo(character2);
equals
Using AI Code Generation
1package org.assertj.core.test;2import java.util.Objects;3public class Name {4 private String first, last;5 public Name(String first, String last) {6 this.first = first;7 this.last = last;8 }9 public String getFirst() {10 return first;11 }12 public String getLast() {13 return last;14 }15 public boolean equals(Object obj) {16 if (this == obj) {17 return true;18 }19 if (!(obj instanceof Name)) {20 return false;21 }22 Name name = (Name) obj;23 return Objects.equals(first, name.first) && Objects.equals(last, name.last);24 }25 public int hashCode() {26 return Objects.hash(first, last);27 }28}29package org.assertj.core.test;30import java.util.Objects;31public class Person {32 private Name name;33 private int age;34 public Person(Name name, int age) {35 this.name = name;36 this.age = age;37 }38 public Name getName() {39 return name;40 }41 public int getAge() {42 return age;43 }44 public boolean equals(Object obj) {45 if (this == obj) {46 return true;47 }48 if (!(obj instanceof Person)) {49 return false;50 }51 Person person = (Person) obj;52 return age == person.age && Objects.equals(name, person.name);53 }54 public int hashCode() {55 return Objects.hash(name, age);56 }57}58package org.assertj.core.test;59import org.assertj.core.api.Assertions;60import org.junit.Test;61public class CustomAssertionTest {62 public void testCustomAssertion() {63 Person actual = new Person(new Name("john", "doe"), 25);64 Person expected = new Person(new Name("john", "doe"), 25);65 Assertions.assertThat(actual).isEqualTo(expected);66 }67}
equals
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import org.assertj.core.test.Name;3class TestClass {4 public static void main(String[] args) {5 Name name1 = new Name("John", "Doe");6 Name name2 = new Name("John", "Doe");7 assertThat(name1).isEqualTo(name2);8 }9}10 at org.junit.Assert.assertEquals(Assert.java:115)11 at org.junit.Assert.assertEquals(Assert.java:144)12 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:81)13 at TestClass.main(TestClass.java:7)14import static org.assertj.core.api.Assertions.assertThat;15import org.assertj.core.test.Name;16class TestClass {17 public static void main(String[] args) {18 Name name1 = new Name("John", "Doe");19 Name name2 = new Name("John", "Doe");20 assertThat(name1).isEqualToIgnoringGivenFields(name2, "first");21 }22}23 at org.junit.Assert.assertEquals(Assert.java:115)24 at org.junit.Assert.assertEquals(Assert.java:144)25 at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:81)26 at TestClass.main(TestClass.java:7)27import static org.assertj.core.api.Assertions.assertThat;28import org.assertj.core.test.Name;29class TestClass {30 public static void main(String[] args) {31 Name name1 = new Name("John", "Doe");32 Name name2 = new Name("John", "Doe");33 assertThat(name1).isEqualToIgnoringGivenFields(name2, "first", "last");34 }35}
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!!