How to use Preconditions class of org.assertj.core.util package

Best Assertj code snippet using org.assertj.core.util.Preconditions

Source:Events.java Github

copy

Full Screen

...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();296 }297}...

Full Screen

Full Screen

Preconditions

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.util.Preconditions;2import org.junit.Test;3import static org.assertj.core.api.Assertions.assertThat;4import static org.assertj.core.api.Assertions.assertThatThrownBy;5public class PreconditionsTest {6 public void shouldThrowExceptionWhenNull() {7 assertThatThrownBy(() -> Preconditions.checkNotNull(null, "message"))8 .isInstanceOf(NullPointerException.class)9 .hasMessage("message");10 }11 public void shouldReturnSameObjectWhenNotNull() {12 assertThat(Preconditions.checkNotNull("test", "message")).isEqualTo("test");13 }14}15 at org.junit.Assert.assertEquals(Assert.java:115)16 at org.junit.Assert.assertEquals(Assert.java:144)17 at org.assertj.core.api.AbstractThrowableAssert.hasMessage(AbstractThrowableAssert.java:188)18 at org.assertj.core.api.Assertions.assertThatThrownBy(Assertions.java:1552)19 at org.assertj.core.api.Assertions.assertThatThrownBy(Assertions.java:1520)20 at org.assertj.core.util.PreconditionsTest.shouldThrowExceptionWhenNull(PreconditionsTest.java:17)

Full Screen

Full Screen

Preconditions

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.util.Preconditions;2import org.junit.Test;3import static org.assertj.core.api.Assertions.assertThat;4public class PreconditionsTest {5 public void shouldThrowExceptionWhenConditionIsFalse() {6 try {7 Preconditions.checkArgument(1 == 2, "Condition is false");8 } catch (IllegalArgumentException e) {9 assertThat(e.getMessage()).isEqualTo("Condition is false");10 }11 }12}

Full Screen

Full Screen

Preconditions

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.util.Preconditions.*;2public void testPreconditions() {3 String s = "Hello";4 checkNotNull(s);5}6import static com.google.common.base.Preconditions.*;7public void testPreconditions() {8 String s = "Hello";9 checkNotNull(s);10}11import static org.apache.commons.lang3.Validate.*;12public void testPreconditions() {13 String s = "Hello";14 notNull(s);15}16import static org.apache.commons.lang.Validate.*;17public void testPreconditions() {18 String s = "Hello";19 notNull(s);20}21import static org.springframework.util.Assert.*;22public void testPreconditions() {23 String s = "Hello";24 notNull(s);25}26import static org.apache.commons.lang.Validate.*;27public void testPreconditions() {28 String s = "Hello";29 notNull(s);30}31import static org.apache.commons.lang.Validate.*;32public void testPreconditions() {33 String s = "Hello";34 notNull(s);35}36import static org.apache.commons.lang.Validate.*;37public void testPreconditions() {38 String s = "Hello";39 notNull(s);40}41import static org.apache.commons.lang.Validate.*;42public void testPreconditions() {43 String s = "Hello";44 notNull(s);45}46import static org.apache.commons.lang.Validate.*;47public void testPreconditions() {48 String s = "Hello";49 notNull(s);50}51import static org.apache.commons.lang.Validate.*;52public void testPreconditions() {53 String s = "Hello";54 notNull(s);55}56import static org.apache.commons.lang.Validate.*;57public void testPreconditions() {58 String s = "Hello";59 notNull(s);60}61import static org.apache

Full Screen

Full Screen

Preconditions

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.util.Preconditions;3public class AssertionDemo {4 public static void main(String[] args) {5 String name = "Ram";6 String address = "Kathmandu";7 int age = 30;8 Preconditions.checkNotNull(name, "Name should not be null");9 Preconditions.checkNotNull(address, "Address should not be null");10 Preconditions.checkArgument(age > 0, "Age should be greater than 0");11 Assertions.assertThat(name).isNotNull();12 Assertions.assertThat(address).isNotNull();13 Assertions.assertThat(age).isGreaterThan(0);14 }15}16 Preconditions.checkArgument(age > 0, "Age should be greater than 0");17 found : @IntVal(30) int18 required: @LessThan("0") int19 Preconditions.checkArgument(age > 0, "Age should be greater than 0");20 found : @IntVal(30) int21 required: @LessThan("0") int22 Preconditions.checkArgument(age > 0, "Age should be greater than 0");23 found : @IntVal(30) int24 required: @LessThan("0") int25 Preconditions.checkArgument(age > 0, "Age should be greater than 0");26 found : @IntVal(30) int27 required: @LessThan("0") int28 Preconditions.checkArgument(age > 0, "Age should be greater than 0");29 found : @IntVal(30) int30 required: @LessThan("0") int

Full Screen

Full Screen

Preconditions

Using AI Code Generation

copy

Full Screen

1package com.journaldev.junit;2import org.assertj.core.util.Preconditions;3public class AssertJPreconditionsTest {4 public static void main(String[] args) {5 String name = "JournalDev";6 int age = 10;7 Preconditions.checkArgument(name != null, "Name should not be null");8 Preconditions.checkArgument(age > 18, "Age should be greater than 18");9 System.out.println("Name is "+name+" and age is "+age);10 }11}12package com.journaldev.junit;13import org.assertj.core.api.Assertions;14public class AssertJAssertionsTest {15 public static void main(String[] args) {16 String name = "JournalDev";17 int age = 10;18 Assertions.assertThat(name).isNotNull();19 Assertions.assertThat(age).isGreaterThan(18);20 System.out.println("Name is "+name+" and age is "+age);21 }22}

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.

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