Skip to content

Commit

Permalink
refactor: WebTestClient使用jsonpath处理返回结果
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed May 11, 2024
1 parent dfbfaef commit 0d72f2c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
Expand Up @@ -16,11 +16,8 @@

package com.livk.crypto.webflux.controller;

import com.fasterxml.jackson.databind.JsonNode;
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 Down Expand Up @@ -56,9 +53,11 @@ void infoGet() {
.isOk()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(JsonNode.class)
.value(jsonNode -> JsonNodeUtils.findNode(jsonNode, "id.paramId").asText(), Is.is(encoding))
.value(jsonNode -> JsonNodeUtils.findNode(jsonNode, "id.headerId").asText(), Is.is(encoding));
.expectBody()
.jsonPath("id.paramId", encoding)
.exists()
.jsonPath("id.headerId", encoding)
.exists();
}

@Test
Expand All @@ -75,9 +74,11 @@ void infoPost() {
.isOk()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(JsonNode.class)
.value(jsonNode -> JsonNodeUtils.findNode(jsonNode, "body.paramId").asText(), Is.is(encoding))
.value(jsonNode -> JsonNodeUtils.findNode(jsonNode, "body.headerId").asText(), Is.is(encoding));
.expectBody()
.jsonPath("body.paramId", encoding)
.exists()
.jsonPath("body.headerId", encoding)
.exists();
}

}
Expand Up @@ -16,8 +16,6 @@

package com.livk.doc.webflux.controller;

import com.fasterxml.jackson.databind.JsonNode;
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 @@ -44,8 +42,9 @@ void test() {
.isOk()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(JsonNode.class)
.value(jsonNode -> jsonNode.get("openapi").asText(), Is.is("3.0.1"));
.expectBody()
.jsonPath("openapi", "3.0.1")
.exists();
}

}
Expand Up @@ -16,8 +16,6 @@

package com.livk.spring.controller;

import com.fasterxml.jackson.databind.JsonNode;
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 Down Expand Up @@ -46,8 +44,9 @@ void greeting() {
.isOk()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody(JsonNode.class)
.value(jsonNode -> jsonNode.get("content").asText(), Is.is("hello,World!"));
.expectBody()
.jsonPath("content", "hello,World!")
.exists();
}

}

0 comments on commit 0d72f2c

Please sign in to comment.