How to use stream method of org.assertj.core.api.ListAssert class

Best Assertj code snippet using org.assertj.core.api.ListAssert.stream

Source:Events.java Github

copy

Full Screen

...8 * https://www.eclipse.org/legal/epl-v20.html9 */10package org.junit.platform.testkit.engine;11import static java.util.function.Predicate.isEqual;12import static java.util.stream.Collectors.toList;13import static org.apiguardian.api.API.Status.EXPERIMENTAL;14import static org.junit.platform.commons.util.FunctionUtils.where;15import static org.junit.platform.testkit.engine.Event.byPayload;16import static org.junit.platform.testkit.engine.Event.byType;17import java.io.OutputStream;18import java.io.PrintWriter;19import java.io.Writer;20import java.util.Collections;21import java.util.List;22import java.util.function.Consumer;23import java.util.function.Function;24import java.util.function.Predicate;25import java.util.stream.Stream;26import org.apiguardian.api.API;27import org.assertj.core.api.Assertions;28import org.assertj.core.api.Condition;29import org.assertj.core.api.ListAssert;30import org.assertj.core.api.SoftAssertions;31import org.assertj.core.data.Index;32import org.junit.platform.commons.util.Preconditions;33import org.junit.platform.engine.TestExecutionResult;34import org.junit.platform.engine.TestExecutionResult.Status;35/**36 * {@code Events} is a facade that provides a fluent API for working with37 * {@linkplain Event events}.38 *39 * @since 1.440 */41@API(status = EXPERIMENTAL, since = "1.4")42public final class Events {43 private final List<Event> events;44 private final String category;45 Events(Stream<Event> events, String category) {46 this(Preconditions.notNull(events, "Event stream must not be null").collect(toList()), category);47 }48 Events(List<Event> events, String category) {49 Preconditions.notNull(events, "Event list must not be null");50 Preconditions.containsNoNullElements(events, "Event list must not contain null elements");51 this.events = Collections.unmodifiableList(events);52 this.category = category;53 }54 String getCategory() {55 return this.category;56 }57 // --- Accessors -----------------------------------------------------------58 /**59 * Get the {@linkplain Event events} as a {@link List}.60 *61 * @return the list of events; never {@code null}62 * @see #stream()63 */64 public List<Event> list() {65 return this.events;66 }67 /**68 * Get the {@linkplain Event events} as a {@link Stream}.69 *70 * @return the stream of events; never {@code null}71 * @see #list()72 */73 public Stream<Event> stream() {74 return this.events.stream();75 }76 /**77 * Shortcut for {@code events.stream().map(mapper)}.78 *79 * @param mapper a {@code Function} to apply to each event; never {@code null}80 * @return the mapped stream of events; never {@code null}81 * @see #stream()82 * @see Stream#map(Function)83 */84 public <R> Stream<R> map(Function<? super Event, ? extends R> mapper) {85 Preconditions.notNull(mapper, "Mapping function must not be null");86 return stream().map(mapper);87 }88 /**89 * Shortcut for {@code events.stream().filter(predicate)}.90 *91 * @param predicate a {@code Predicate} to apply to each event to decide if92 * it should be included in the filtered stream; never {@code null}93 * @return the filtered stream of events; never {@code null}94 * @see #stream()95 * @see Stream#filter(Predicate)96 */97 public Stream<Event> filter(Predicate<? super Event> predicate) {98 Preconditions.notNull(predicate, "Filter predicate must not be null");99 return stream().filter(predicate);100 }101 /**102 * Get the {@link Executions} for the current set of {@linkplain Event events}.103 *104 * @return an instance of {@code Executions} for the current set of events;105 * never {@code null}106 */107 public Executions executions() {108 return new Executions(this.events, this.category);109 }110 // --- Statistics ----------------------------------------------------------111 /**112 * Get the number of {@linkplain Event events} contained in this {@code Events}113 * object.114 */115 public long count() {116 return this.events.size();117 }118 // --- Built-in Filters ----------------------------------------------------119 /**120 * Get the skipped {@link Events} contained in this {@code Events} object.121 *122 * @return the filtered {@code Events}; never {@code null}123 */124 public Events skipped() {125 return new Events(eventsByType(EventType.SKIPPED), this.category + " Skipped");126 }127 /**128 * Get the started {@link Events} contained in this {@code Events} object.129 *130 * @return the filtered {@code Events}; never {@code null}131 */132 public Events started() {133 return new Events(eventsByType(EventType.STARTED), this.category + " Started");134 }135 /**136 * Get the finished {@link Events} contained in this {@code Events} object.137 *138 * @return the filtered {@code Events}; never {@code null}139 */140 public Events finished() {141 return new Events(eventsByType(EventType.FINISHED), this.category + " Finished");142 }143 /**144 * Get the aborted {@link Events} contained in this {@code Events} object.145 *146 * @return the filtered {@code Events}; never {@code null}147 */148 public Events aborted() {149 return new Events(finishedEventsByStatus(Status.ABORTED), this.category + " Aborted");150 }151 /**152 * Get the succeeded {@link Events} contained in this {@code Events} object.153 *154 * @return the filtered {@code Events}; never {@code null}155 */156 public Events succeeded() {157 return new Events(finishedEventsByStatus(Status.SUCCESSFUL), this.category + " Successful");158 }159 /**160 * Get the failed {@link Events} contained in this {@code Events} object.161 *162 * @return the filtered {@code Events}; never {@code null}163 */164 public Events failed() {165 return new Events(finishedEventsByStatus(Status.FAILED), this.category + " Failed");166 }167 /**168 * Get the reporting entry publication {@link Events} contained in this169 * {@code Events} object.170 *171 * @return the filtered {@code Events}; never {@code null}172 */173 public Events reportingEntryPublished() {174 return new Events(eventsByType(EventType.REPORTING_ENTRY_PUBLISHED),175 this.category + " Reporting Entry Published");176 }177 /**178 * Get the dynamic registration {@link Events} contained in this179 * {@code Events} object.180 *181 * @return the filtered {@code Events}; never {@code null}182 */183 public Events dynamicallyRegistered() {184 return new Events(eventsByType(EventType.DYNAMIC_TEST_REGISTERED), this.category + " Dynamically Registered");185 }186 // --- Assertions ----------------------------------------------------------187 /**188 * Assert statistics for the {@linkplain Event events} contained in this189 * {@code Events} object.190 *191 * <h4>Example</h4>192 *193 * <p>{@code events.assertStatistics(stats -> stats.started(1).succeeded(1).failed(0));}194 *195 * @param statisticsConsumer a {@link Consumer} of {@link EventStatistics};196 * never {@code null}197 * @return this {@code Events} object for method chaining; never {@code null}198 */199 public Events assertStatistics(Consumer<EventStatistics> statisticsConsumer) {200 Preconditions.notNull(statisticsConsumer, "Consumer must not be null");201 EventStatistics eventStatistics = new EventStatistics(this, this.category);202 statisticsConsumer.accept(eventStatistics);203 eventStatistics.assertAll();204 return this;205 }206 /**207 * Assert that all {@linkplain Event events} contained in this {@code Events}208 * object exactly match the provided conditions.209 *210 * <p>Conditions can be imported statically from {@link EventConditions}211 * and {@link TestExecutionResultConditions}.212 *213 * <h4>Example</h4>214 *215 * <pre class="code">216 * executionResults.tests().assertEventsMatchExactly(217 * event(test("exampleTestMethod"), started()),218 * event(test("exampleTestMethod"), finishedSuccessfully())219 * );220 * </pre>221 *222 * @param conditions the conditions to match against; never {@code null}223 * @see EventConditions224 * @see TestExecutionResultConditions225 */226 @SafeVarargs227 public final void assertEventsMatchExactly(Condition<? super Event>... conditions) {228 Preconditions.notNull(conditions, "conditions must not be null");229 assertEventsMatchExactly(this.events, conditions);230 }231 /**232 * Shortcut for {@code org.assertj.core.api.Assertions.assertThat(events.list())}.233 *234 * @return an instance of {@link ListAssert} for events; never {@code null}235 * @see org.assertj.core.api.Assertions#assertThat(List)236 * @see org.assertj.core.api.ListAssert237 */238 public ListAssert<Event> assertThatEvents() {239 return org.assertj.core.api.Assertions.assertThat(list());240 }241 // --- Diagnostics ---------------------------------------------------------242 /**243 * Print all events to {@link System#out}.244 *245 * @return this {@code Events} object for method chaining; never {@code null}246 */247 public Events debug() {248 debug(System.out);249 return this;250 }251 /**252 * Print all events to the supplied {@link OutputStream}.253 *254 * @param out the {@code OutputStream} to print to; never {@code null}255 * @return this {@code Events} object for method chaining; never {@code null}256 */257 public Events debug(OutputStream out) {258 Preconditions.notNull(out, "OutputStream must not be null");259 debug(new PrintWriter(out, true));260 return this;261 }262 /**263 * Print all events to the supplied {@link Writer}.264 *265 * @param writer the {@code Writer} to print to; never {@code null}266 * @return this {@code Events} object for method chaining; never {@code null}267 */268 public Events debug(Writer writer) {269 Preconditions.notNull(writer, "Writer must not be null");270 debug(new PrintWriter(writer, true));271 return this;272 }273 private Events debug(PrintWriter printWriter) {274 printWriter.println(this.category + " Events:");275 this.events.forEach(event -> printWriter.printf("\t%s%n", event));276 return this;277 }278 // --- Internals -----------------------------------------------------------279 private Stream<Event> eventsByType(EventType type) {280 Preconditions.notNull(type, "EventType must not be null");281 return stream().filter(byType(type));282 }283 private Stream<Event> finishedEventsByStatus(Status status) {284 Preconditions.notNull(status, "Status must not be null");285 return eventsByType(EventType.FINISHED)//286 .filter(byPayload(TestExecutionResult.class, where(TestExecutionResult::getStatus, isEqual(status))));287 }288 @SafeVarargs289 private static void assertEventsMatchExactly(List<Event> events, Condition<? super Event>... conditions) {290 Assertions.assertThat(events).hasSize(conditions.length);291 SoftAssertions softly = new SoftAssertions();292 for (int i = 0; i < conditions.length; i++) {293 softly.assertThat(events).has(conditions[i], Index.atIndex(i));294 }295 softly.assertAll();...

Full Screen

Full Screen

Source:FlinkAssertions.java Github

copy

Full Screen

...22import org.assertj.core.api.InstanceOfAssertFactory;23import org.assertj.core.api.ListAssert;24import org.assertj.core.api.ThrowingConsumer;25import java.util.function.Function;26import java.util.stream.Stream;27import static org.assertj.core.api.Assertions.assertThat;28/** Some reusable assertions and utilities for AssertJ. */29public final class FlinkAssertions {30 private FlinkAssertions() {}31 /** @see #chainOfCauses(Throwable) */32 @SuppressWarnings({"rawtypes", "unused"})33 public static final InstanceOfAssertFactory<Stream, ListAssert<Throwable>> STREAM_THROWABLE =34 new InstanceOfAssertFactory<>(Stream.class, Assertions::<Throwable>assertThat);35 /**36 * Shorthand to assert the chain of causes includes a {@link Throwable} matching a specific37 * {@link Class} and containing the provided message. Same as:38 *39 * <pre>{@code40 * assertThatChainOfCauses(throwable)...

Full Screen

Full Screen

Source:AbstractStreamAssert.java Github

copy

Full Screen

1package de.axone.test;2import java.util.List;3import java.util.stream.Collectors;4import java.util.stream.Stream;5import org.assertj.core.api.AbstractObjectAssert;6import org.assertj.core.api.ListAssert;7public abstract class AbstractStreamAssert<8 X,9 S extends AbstractObjectAssert<S, Stream<X>>10> extends AbstractObjectAssert<S,Stream<X>> {11 protected AbstractStreamAssert( Stream<X> actual ) {12 super( actual, AbstractStreamAssert.class );13 }14 15 public S hasSize( int size ) {16 17 org.assertj.core.api.Assertions.assertThat( actual.count() )18 .as( descriptionText() )...

Full Screen

Full Screen

stream

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.ListAssert;2public class 1 {3 public static void main(String[] args) {4 ListAssert<Integer> listAssert = new ListAssert<Integer>(Arrays.asList(1, 2, 3));5 listAssert.stream().allMatch(i -> i > 0);6 }7}

Full Screen

Full Screen

stream

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.api;2public class ListAssert<ELEMENT> extends AbstractListAssert<ListAssert<ELEMENT>, List<ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>> {3 public ListAssert(List<ELEMENT> actual) {4 super(actual, ListAssert.class);5 }6 public ListAssert<ELEMENT> filteredOn(Predicate<? super ELEMENT> filter) {7 return usingElementComparatorOnFields().filteredOn(filter);8 }9}10package org.assertj.core.api;11public class ListAssert<ELEMENT> extends AbstractListAssert<ListAssert<ELEMENT>, List<ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>> {12 public ListAssert(List<ELEMENT> actual) {13 super(actual, ListAssert.class);14 }15 public ListAssert<ELEMENT> filteredOn(Predicate<? super ELEMENT> filter) {16 return usingElementComparatorOnFields().filteredOn(filter);17 }18}19package org.assertj.core.api;20public class ListAssert<ELEMENT> extends AbstractListAssert<ListAssert<ELEMENT>, List<ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>> {21 public ListAssert(List<ELEMENT> actual) {22 super(actual, ListAssert.class);23 }24 public ListAssert<ELEMENT> filteredOn(Predicate<? super ELEMENT> filter) {25 return usingElementComparatorOnFields().filteredOn(filter);26 }27}28package org.assertj.core.api;29public class ListAssert<ELEMENT> extends AbstractListAssert<ListAssert<ELEMENT>, List<ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>> {30 public ListAssert(List<ELEMENT> actual) {31 super(actual, ListAssert.class);32 }33 public ListAssert<ELEMENT> filteredOn(Predicate<? super ELEMENT> filter) {34 return usingElementComparatorOnFields().filteredOn(filter);35 }36}37package org.assertj.core.api;38public class ListAssert<ELEMENT> extends AbstractListAssert<ListAssert<ELEMENT>, List<ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>> {39 public ListAssert(List<ELEMENT> actual) {40 super(actual, ListAssert.class);41 }

Full Screen

Full Screen

stream

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.ListAssert;2import java.util.List;3import java.util.Arrays;4public class 1 {5 public static void main(String[] args) {6 List<String> list1 = Arrays.asList("a", "b", "c");7 List<String> list2 = Arrays.asList("a", "b", "c");8 ListAssert listAssert = new ListAssert(list1);9 listAssert.containsExactlyElementsOf(list2);10 }11}12at org.assertj.core.api.ListAssert.containsExactlyElementsOf(ListAssert.java:552)13at org.assertj.core.api.ListAssert.containsExactlyElementsOf(ListAssert.java:51)14at 1.main(1.java:12)

Full Screen

Full Screen

stream

Using AI Code Generation

copy

Full Screen

1package com.ack.j2se.assertj;2import org.assertj.core.api.ListAssert;3import org.junit.Test;4import java.util.Arrays;5import java.util.List;6public class ListAssertTest {7 public void testListAssert() {8 List<String> list = Arrays.asList( "a", "b", "c" );9 ListAssert<String> listAssert = new ListAssert<>( list );10 listAssert.isNotNull()11 .isNotEmpty()12 .hasSize( 3 )13 .contains( "a", "b", "c" )14 .containsOnlyOnce( "a", "b", "c" )15 .doesNotContain( "d" )16 .doesNotHaveDuplicates()17 .startsWith( "a" )18 .endsWith( "c" );19 }20}21package com.ack.j2se.assertj;22import org.assertj.core.api.MapAssert;23import org.junit.Test;24import java.util.HashMap;25import java.util.Map;26public class MapAssertTest {27 public void testMapAssert() {28 Map<String, String> map = new HashMap<>();29 map.put( "a", "1" );30 map.put( "b", "2" );31 map.put( "c", "3" );32 MapAssert<String, String> mapAssert = new MapAssert<>( map );33 mapAssert.isNotNull()34 .isNotEmpty()35 .hasSize( 3 )36 .containsKeys( "a", "b", "c" )37 .containsValues( "1", "2", "3" )38 .containsEntry( "a", "1" )39 .containsOnlyKeys( "a", "b", "c" )40 .containsOnlyValues( "1", "2", "3" )41 .containsExactly( map );42 }43}44package com.ack.j2se.assertj;45import org.assertj.core.api.SetAssert;46import org.junit.Test;47import java.util.HashSet;48import java.util.Set;49public class SetAssertTest {50 public void testSetAssert() {51 Set<String> set = new HashSet<>();52 set.add( "a" );

Full Screen

Full Screen

stream

Using AI Code Generation

copy

Full Screen

1import java.util.Arrays;2import java.util.List;3import org.assertj.core.api.ListAssert;4import org.assertj.core.api.ListAssertBaseTest;5import org.junit.jupiter.api.Test;6import static org.mockito.Mockito.verify;7public class ListAssert_sorted_Test extends ListAssertBaseTest {8 protected ListAssert<Object> invoke_api_method() {9 return assertions.sorted();10 }11 protected void verify_internal_effects() {12 verify(lists).assertIsSorted(getInfo(assertions), getActual(assertions));13 }14 public void should_pass_if_list_is_sorted() {15 List<Integer> actual = Arrays.asList(1, 2, 3, 4);16 new ListAssert<>(actual).sorted();17 }18}19 verify(lists).assertIsSorted(getInfo(assertions), getActual(assertions));

Full Screen

Full Screen

stream

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import java.util.Arrays;3import java.util.List;4import static org.assertj.core.api.Assertions.assertThat;5public class ListAssertTest {6 public void testListAssert() {7 List<String> list = Arrays.asList("one", "two", "three", "four");8 assertThat(list).contains("one", "two", "three");9 }10}

Full Screen

Full Screen

stream

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.ListAssert;2import org.assertj.core.api.Assertions;3import java.util.ArrayList;4import java.util.List;5import org.junit.Test;6public class AssertjExample {7 public void test1() {8 List<String> list = new ArrayList<>();9 list.add("apple");10 list.add("banana");11 list.add("orange");12 list.add("mango");13 list.add("grapes");14 ListAssert<String> assertList = Assertions.assertThat(list);15 assertList.stream().anyMatch("apple"::equals);16 }17}18at org.assertj.core.api.ListAssert.stream(ListAssert.java:466)19at AssertjExample.test1(AssertjExample.java:19)20org.assertj.core.api.ListAssert.stream(ListAssert.java:466)21assertList.stream().anyMatch("apple"::equals);22How to use assertThrows() method to test exceptions in JUnit5?23How to use assertDoesNotThrow() method to test exceptions in JUnit5?24How to use assertAll() method to test exceptions in JUnit5?25How to use assertTimeout() method to test exceptions in JUnit5?26How to use assertTimeoutPreemptively() method to test exceptions in JUnit5?27How to use assertIterableEquals() method to test exceptions in JUnit5?28How to use assertArrayEquals() method to test exceptions in JUnit5?29How to use assertEquals() method to test exceptions in JUnit5?30How to use assertNotEquals() method to test exceptions in JUnit5?31How to use assertNull() method to test exceptions in JUnit5?32How to use assertNotNull() method to test exceptions in JUnit5?33How to use assertSame() method to test exceptions in JUnit5?34How to use assertNotSame() method

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful