日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

junit junit_JSON的JUnit Hamcrest Matcher

發布時間:2023/12/3 javascript 99 豆豆
生活随笔 收集整理的這篇文章主要介紹了 junit junit_JSON的JUnit Hamcrest Matcher 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

junit junit

這篇文章展示了如何編寫JUnit測試來檢查對象是否與JSON字符串匹配。 如果您要實現REST服務并想測試您的服務是否產生了預期的JSON響應,那么這非常重要。

JSONassert是比較JSON對象的有用庫。 首先,您必須將Java對象轉換為JSON字符串(例如,使用Jackson ),然后使用JSONassert將其與所需的JSON字符串進行比較。 (您也可以將Java對象轉換為JSONObject但我發現將其轉換為字符串要容易得多。)

以下代碼段顯示了如何使用JSONassert將對象(在這種情況下為List )與其JSON表示形式進行比較。

import org.skyscreamer.jsonassert.JSONAssert; import com.fasterxml.jackson.databind.ObjectMapper;List<String> fruits = Arrays.asList("apple", "banana"); String fruitsJSON = new ObjectMapper().writeValueAsString(fruits); String expectedFruitsJSON = "[\"apple\", \"banana\"]"; JSONAssert.assertEquals(expectedFruitsJSON, fruitsJSON, true);

為了簡化編寫此類單元測試的過程,我編寫了一個名為IsEqualJSON的Hamcrest Matcher,用于比較JSON對象。 它仍然使用JSONassert,但允許您以更流暢的方式表達測試。

以下代碼顯示了如何使用IsEqualJSON :

import static org.junit.Assert.*; import static testutil.IsEqualJSON.*;assertThat(Arrays.asList("apple", "banana"),equalToJSON("[\"apple\", \"banana\"]"));// you can also have your expected JSON read from a file assertThat(Arrays.asList("apple", "banana"),equalToJSONInFile("fruits.json"));

這是IsEqualJSON的代碼(也在我的GitHub Repository中提供 ):

import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.*; import org.hamcrest.*; import org.skyscreamer.jsonassert.*; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper;/*** A Matcher for comparing JSON.* Example usage:* <pre>* assertThat(new String[] {"foo", "bar"}, equalToJSON("[\"foo\", \"bar\"]"));* assertThat(new String[] {"foo", "bar"}, equalToJSONInFile("/tmp/foo.json"));* </pre>*/ public class IsEqualJSON extends DiagnosingMatcher<Object> {private final String expectedJSON;private JSONCompareMode jsonCompareMode;public IsEqualJSON(final String expectedJSON) {this.expectedJSON = expectedJSON;this.jsonCompareMode = JSONCompareMode.STRICT;}@Overridepublic void describeTo(final Description description) {description.appendText(expectedJSON);}@Overrideprotected boolean matches(final Object actual,final Description mismatchDescription) {final String actualJSON = toJSONString(actual);final JSONCompareResult result = JSONCompare.compareJSON(expectedJSON,actualJSON,jsonCompareMode);if (!result.passed()) {mismatchDescription.appendText(result.getMessage());}return result.passed();}private static String toJSONString(final Object o) {try {return o instanceof String ?(String) o : new ObjectMapper().writeValueAsString(o);} catch (final JsonProcessingException e) {throw new RuntimeException(e);}}private static String getFileContents(final Path path) {try {return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);} catch (final IOException e) {throw new RuntimeException(e);}}@Factorypublic static IsEqualJSON equalToJSON(final String expectedJSON) {return new IsEqualJSON(expectedJSON);}@Factorypublic static IsEqualJSON equalToJSONInFile(final Path expectedPath) {return equalToJSON(getFileContents(expectedPath));}@Factorypublic static IsEqualJSON equalToJSONInFile(final String expectedFileName) {return equalToJSONInFile(Paths.get(expectedFileName));} }

翻譯自: https://www.javacodegeeks.com/2018/03/junit-hamcrest-matcher-for-json.html

junit junit

總結

以上是生活随笔為你收集整理的junit junit_JSON的JUnit Hamcrest Matcher的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。