javascript
junit junit_JSON的JUnit Hamcrest Matcher
junit junit
這篇文章展示了如何編寫(xiě)JUnit測(cè)試來(lái)檢查對(duì)象是否與JSON字符串匹配。 如果您要實(shí)現(xiàn)REST服務(wù)并想測(cè)試您的服務(wù)是否產(chǎn)生了預(yù)期的JSON響應(yīng),那么這非常重要。
JSONassert是比較JSON對(duì)象的有用庫(kù)。 首先,您必須將Java對(duì)象轉(zhuǎn)換為JSON字符串(例如,使用Jackson ),然后使用JSONassert將其與所需的JSON字符串進(jìn)行比較。 (您也可以將Java對(duì)象轉(zhuǎn)換為JSONObject但我發(fā)現(xiàn)將其轉(zhuǎn)換為字符串要容易得多。)
以下代碼段顯示了如何使用JSONassert將對(duì)象(在這種情況下為L(zhǎng)ist )與其JSON表示形式進(jìn)行比較。
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);為了簡(jiǎn)化編寫(xiě)此類(lèi)單元測(cè)試的過(guò)程,我編寫(xiě)了一個(gè)名為IsEqualJSON的Hamcrest Matcher,用于比較JSON對(duì)象。 它仍然使用JSONassert,但允許您以更流暢的方式表達(dá)測(cè)試。
以下代碼顯示了如何使用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
總結(jié)
以上是生活随笔為你收集整理的junit junit_JSON的JUnit Hamcrest Matcher的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 电脑芯片组是什么(主板上的芯片组是什么芯
- 下一篇: 749局是真实存在的吗(中国749局简介