[Reactor Practice] StepVerifier

kimchj·2024년 3월 4일

Reactor

목록 보기
3/11

StepVerifier.create(Flux):

StepVerifier.create(Flux)
			.expectNext(data) 	// Expect the next elements received to be equal to the given values.
            .verifyComplete();	// Trigger the verification, expecting a completion signal as terminal event.
            // .expectComplete()
            // .verify();

StepVerifier Reactor Practice

/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.pivotal.literx;

//generic imports to help with simpler IDEs (ie tech.io)
import java.util.*;
import java.util.function.*;
import java.time.*;
import reactor.test.StepVerifier;

import io.pivotal.literx.domain.User;
import reactor.core.publisher.Flux;

import static org.assertj.core.api.Assertions.*;

/**
 * Learn how to use StepVerifier to test Mono, Flux or any other kind of Reactive Streams Publisher.
 *
 * @author Sebastien Deleuze
 * @see <a href="https://projectreactor.io/docs/test/release/api/reactor/test/StepVerifier.html">StepVerifier Javadoc</a>
 */
public class Part03StepVerifier {

//========================================================================================

	// TODO Use StepVerifier to check that the flux parameter emits "foo" and "bar" elements then completes successfully.
	void expectFooBarComplete(Flux<String> flux) {
        StepVerifier.create(flux)
                    .expectNext("foo")
                    .expectNext("bar")
                    .verifyComplete();
	}

//========================================================================================

	// TODO Use StepVerifier to check that the flux parameter emits "foo" and "bar" elements then a RuntimeException error.
	void expectFooBarError(Flux<String> flux) {
        StepVerifier.create(flux)
                    .expectNext("foo")
                    .expectNext("bar")
                    .expectError();
	}

//========================================================================================

	// TODO Use StepVerifier to check that the flux parameter emits a User with "swhite"username
	// and another one with "jpinkman" then completes successfully.
	void expectSkylerJesseComplete(Flux<User> flux) {
        StepVerifier.create(flux)
                    .assertNext(user -> {
                        assertThat(user.getUsername()).isEqualTo("swhite");
                    })
                    .assertNext(user -> {
                        assertThat(user.getUsername()).isEqualTo("jpinkman");
                    })
                    .verifyComplete();
	}

//========================================================================================

	// TODO Expect 10 elements then complete and notice how long the test takes.
	void expect10Elements(Flux<Long> flux) {
        StepVerifier.create(flux)
                    .expectNextCount(10)
                    .verifyComplete();
	}

//========================================================================================

	// TODO Expect 3600 elements at intervals of 1 second, and verify quicker than 3600s
	// by manipulating virtual time thanks to StepVerifier#withVirtualTime, notice how long the test takes
	void expect3600Elements(Supplier<Flux<Long>> supplier) {
		StepVerifier.withVirtualTime(supplier)
            .thenAwait(Duration.ofSeconds(3600))
            .expectNextCount(3600)
            .expectComplete()
            .verify();
	}

	private void fail() {
		throw new AssertionError("workshop not implemented");
	}

}
profile
ㅎㅇ

0개의 댓글