Skip to content

Commit

Permalink
test: WebTestClient使用Matcher处理
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed Dec 5, 2023
1 parent cda02e6 commit 392d48e
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.livk.ck.r2dbc.controller;

import com.livk.ck.r2dbc.entity.User;
import org.hamcrest.core.Is;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
Expand All @@ -29,8 +30,7 @@
import org.springframework.test.web.reactive.server.WebTestClient;

import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.util.List;

/**
* <p>
Expand All @@ -56,7 +56,7 @@ void testUsers() {
.expectStatus()
.isOk()
.expectBodyList(User.class)
.value(users -> assertNotEquals(0, users.size()));
.value(List::size, Is.is(0));
}

@Order(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.livk.commons.jackson.util.JsonNodeUtils;
import com.livk.crypto.CryptoType;
import com.livk.crypto.support.PbeSecurity;
import org.hamcrest.core.Is;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
Expand All @@ -31,8 +32,6 @@
import java.util.Locale;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author livk
*/
Expand All @@ -59,10 +58,8 @@ void infoGet() {
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(JsonNode.class)
.value(jsonNode -> {
assertEquals(encoding, JsonNodeUtils.findNode(jsonNode, "id.paramId").asText());
assertEquals(encoding, JsonNodeUtils.findNode(jsonNode, "id.headerId").asText());
});
.value(jsonNode -> JsonNodeUtils.findNode(jsonNode, "id.paramId").asText(), Is.is(encoding))
.value(jsonNode -> JsonNodeUtils.findNode(jsonNode, "id.headerId").asText(), Is.is(encoding));
}

@Test
Expand All @@ -80,10 +77,8 @@ void infoPost() {
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(JsonNode.class)
.value(jsonNode -> {
assertEquals(encoding, JsonNodeUtils.findNode(jsonNode, "body.paramId").asText());
assertEquals(encoding, JsonNodeUtils.findNode(jsonNode, "body.headerId").asText());
});
.value(jsonNode -> JsonNodeUtils.findNode(jsonNode, "body.paramId").asText(), Is.is(encoding))
.value(jsonNode -> JsonNodeUtils.findNode(jsonNode, "body.headerId").asText(), Is.is(encoding));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
package com.livk.doc.webflux.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.livk.commons.jackson.util.JsonNodeUtils;
import org.hamcrest.core.Is;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author livk
*/
Expand All @@ -48,7 +46,7 @@ public void test() {
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(JsonNode.class)
.value(jsonNode -> assertEquals("3.0.1", JsonNodeUtils.findNode(jsonNode, "openapi").asText()));
.value(jsonNode -> jsonNode.get("openapi").asText(), Is.is("3.0.1"));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.livk.spring.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.livk.commons.jackson.util.JsonNodeUtils;
import org.hamcrest.core.Is;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebFlux;
Expand All @@ -10,8 +10,6 @@
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author livk
*/
Expand All @@ -33,9 +31,7 @@ void greeting() {
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(JsonNode.class)
.value(jsonNode -> {
assertEquals("hello,World!", JsonNodeUtils.findNode(jsonNode, "content").asText());
});
.value(jsonNode -> jsonNode.get("content").asText(), Is.is("hello,World!"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.stream.LongStream;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand All @@ -59,49 +60,41 @@ public static void before() {
}

@Test
void uploadAndDownload() {
client.post()
void uploadAndDownload() throws IOException {
Resource resource = client.post()
.uri("/info/uploadAndDownload")
.bodyValue(builder.build())
.exchange()
.expectStatus()
.isOk()
.expectBody(Resource.class)
.value(resource -> {
try {
FileUtils.download(resource.getInputStream(),
"./infoUploadDownLoad" + ResponseExcel.Suffix.XLSM.getName());
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
.returnResult()
.getResponseBody();

assertNotNull(resource);
FileUtils.download(resource.getInputStream(), "./infoUploadDownLoad" + ResponseExcel.Suffix.XLSM.getName());
File outFile = new File("./infoUploadDownLoad" + ResponseExcel.Suffix.XLSM.getName());
assertTrue(outFile.exists());
assertTrue(outFile.delete());
}

@Test
void download() {
void download() throws IOException {
List<Info> infos = LongStream.rangeClosed(1, 100)
.mapToObj(i -> new Info(i, String.valueOf(13_000_000_000L + i)))
.toList();
client.post()
Resource resource = client.post()
.uri("/info/download")
.bodyValue(infos)
.exchange()
.expectStatus()
.isOk()
.expectBody(Resource.class)
.value(resource -> {
try {
FileUtils.download(resource.getInputStream(),
"./infoDownload" + ResponseExcel.Suffix.XLSM.getName());
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
.returnResult()
.getResponseBody();

assertNotNull(resource);
FileUtils.download(resource.getInputStream(), "./infoDownload" + ResponseExcel.Suffix.XLSM.getName());
File outFile = new File("./infoDownload" + ResponseExcel.Suffix.XLSM.getName());
assertTrue(outFile.exists());
assertTrue(outFile.delete());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.File;
import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand Down Expand Up @@ -81,69 +82,57 @@ void uploadMono() {
}

@Test
void uploadDownLoadMono() {
client.post()
void uploadDownLoadMono() throws IOException {
Resource resource = client.post()
.uri("/uploadDownLoad")
.bodyValue(builder.build())
.exchange()
.expectStatus()
.isOk()
.expectBody(Resource.class)
.value(resource -> {
try {
FileUtils.download(resource.getInputStream(),
"./uploadDownLoad" + ResponseExcel.Suffix.XLS.getName());
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
.returnResult()
.getResponseBody();

assertNotNull(resource);
FileUtils.download(resource.getInputStream(), "./uploadDownLoad" + ResponseExcel.Suffix.XLS.getName());
File outFile = new File("./uploadDownLoad" + ResponseExcel.Suffix.XLS.getName());
assertTrue(outFile.exists());
assertTrue(outFile.delete());
}

@Test
void testUploadDownLoadMono() {
client.post()
void testUploadDownLoadMono() throws IOException {
Resource resource = client.post()
.uri("/uploadDownLoadMono")
.bodyValue(builder.build())
.exchange()
.expectStatus()
.isOk()
.expectBody(Resource.class)
.value(resource -> {
try {
FileUtils.download(resource.getInputStream(),
"./uploadDownLoadMono" + ResponseExcel.Suffix.XLS.getName());
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
.returnResult()
.getResponseBody();

assertNotNull(resource);
FileUtils.download(resource.getInputStream(), "./uploadDownLoadMono" + ResponseExcel.Suffix.XLS.getName());
File outFile = new File("./uploadDownLoadMono" + ResponseExcel.Suffix.XLS.getName());
assertTrue(outFile.exists());
assertTrue(outFile.delete());
}

@Test
void uploadDownLoadFlux() {
client.post()
void uploadDownLoadFlux() throws IOException {
Resource resource = client.post()
.uri("/uploadDownLoadFlux")
.bodyValue(builder.build())
.exchange()
.expectStatus()
.isOk()
.expectBody(Resource.class)
.value(resource -> {
try {
FileUtils.download(resource.getInputStream(),
"./uploadDownLoadFlux" + ResponseExcel.Suffix.XLS.getName());
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
.returnResult()
.getResponseBody();

assertNotNull(resource);
FileUtils.download(resource.getInputStream(), "./uploadDownLoadFlux" + ResponseExcel.Suffix.XLS.getName());
File outFile = new File("./uploadDownLoadFlux" + ResponseExcel.Suffix.XLS.getName());
assertTrue(outFile.exists());
assertTrue(outFile.delete());
Expand Down
Loading

0 comments on commit 392d48e

Please sign in to comment.