반응형
Spring Reactive 를 위해 flux and Mono 에 대해 간단하게 테스트 해보도록 하고 기록으로 남기도록 한다.
private final static String[] countries = new String[]{"Korea","China","America","Canada"};
private final static Flux<String> countiesFlux = Flux.just("Korea","China","America","Canada");
/**
* create flux
*/
@Test
public void createFluxWithJustAndVerify(){
//subscribe 를 사용하여 구독 및 출력 하도록 한다.
countiesFlux.subscribe(s->{
System.out.println(s);
});
//subscription 데이터 개수가 4개인지 검증
StepVerifier.create(countiesFlux)
.expectNextCount(4)
.verifyComplete();
//구독시 순차적으로 구독 하기 때문에 생성 순서대로 expect value 를 설정 하도록 해야한다.
StepVerifier.create(countiesFlux)
.expectNext("Korea")
.expectNext("China")
.expectNext("America")
.expectNext("Canada")
.verifyComplete();
}
/**
* 배열로부터 flux 데이터 생성.
*/
@Test
public void createFluxFromArrayAndVerify(){
// 배열 데이터로부터 flux 생성
Flux<String> flux = Flux.fromArray(countries);
//구독 및 출력
flux.subscribe(System.out::println);
//구독할 데이터의 개수 검증
StepVerifier.create(flux)
.expectNextCount(countries.length)
.verifyComplete();
}
/**
* flux 에 특정 단어들이 포함되어있는지 확인.
*/
@Test
public void containTest(){
Mono<Boolean> hasAllKorean = countiesFlux.all(c -> c.contains("a"));
Mono<Boolean> hasAnyKorean = countiesFlux.any(c -> c.contains("a"));
StepVerifier.create(hasAllKorean)
.expectNext(true)
.verifyComplete();
StepVerifier.create(hasAnyKorean)
.expectNext(true)
.verifyComplete();
}
/**
* filter Test
*/
@Test
public void filterTest(){
Flux<String> filteredCountries = countiesFlux.filter(c -> !c.equalsIgnoreCase("China"));
StepVerifier.create(filteredCountries)
.expectNext("Korea","America","Canada")
.verifyComplete();
}
/**
* distinct Test
*/
@Test
public void distinctTest(){
Flux<String> flux = Flux.just("Korea","Korea","China","America","Canada");
Flux<String> distinctCountries = flux.distinct();
StepVerifier.create(distinctCountries)
.expectNext("Korea","China","America","Canada")
.verifyComplete();
}
반응형
'SpringBoot' 카테고리의 다른 글
React Stream create and test (0) | 2020.08.24 |
---|---|
Spring boot kafka demo with docker-compose (0) | 2020.07.20 |
how to generate random number or Alphabetic in java (0) | 2020.04.30 |
Object List multiple field compare and remove (0) | 2020.02.25 |
Java Object Stream group by multiple field and map in map to list (0) | 2020.01.05 |