MvcResult result = this.mockMvc.perform(
MockMvcRequestBuilders.get(mockUrl))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType("application/ json;charset=UTF-8"))
.andDo(MockMvcResultHandlers.print())
以下を教えてください:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {"version":"0.1"}
Forwarded URL = null
Redirected URL = null
Cookies = []
ただし、それをテストする
MvcResult result = this.mockMvc.perform(
MockMvcRequestBuilders.get(mockUrl))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType("application/ json;charset=UTF-8"))
.andExpect(jsonPath("$.version").value("0.1"))
次のエラーを返します。
Java.lang.AssertionError: No value at JSON path "$.version", exception: net/minidev/json/writer/JsonReaderI
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.Java:245)
at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.Java:99)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers$2.match(JsonPathResultMatchers.Java:99)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.Java:171)
at com.vmware.skyscraper.rts.runbooks.RunbookControllerTest.testGetSingleRunbook(RunbookControllerTest.Java:93)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
at Java.lang.reflect.Method.invoke(Method.Java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.Java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.Java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.Java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.Java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.Java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.Java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.Java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.Java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.Java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.Java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.Java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.Java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.Java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.Java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.Java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.Java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.Java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.Java:70)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
at Java.lang.reflect.Method.invoke(Method.Java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.Java:147)
JsonPathを使用するにはどうすればよいですか?
MatcherをjsonPath
メソッドに渡す必要があると思います。例えば。
.andExpect(jsonPath("$.version", is("0.1")))
あなたのコードは私のためにうまく機能します。私はジャクソンを使用してケースを解析していますが、それがおそらく唯一の違いです。私のコードを確認してください:
結果クラス:
public class Res {
String version;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
コントローラ:
@RequestMapping(value = "/blah",
method = GET,
produces = APPLICATION_JSON_VALUE)
public HttpEntity<Res> doIt() {
Res res = new Res();
res.setVersion("0.1");
return new HttpEntity<>(res);
}
テスト:
@Test
public void blahTest() throws Exception {
this.mockMvc.perform(
MockMvcRequestBuilders.get("/blah"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.version").value("0.1"))
.andDo(MockMvcResultHandlers.print());
}
応答:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {"version":"0.1"}
Forwarded URL = null
Redirected URL = null
Cookies = []
使用しているJsonパーサーライブラリを変更することのみをお勧めします。それ以外の場合は、問題のシンプルで最小限の再現可能な例を作成するために必要なすべての部分でコードを更新してください。
この回答のクレジットはすべて@pramodhに送られます(OPのコメントに隠されています)
私はまったく同じ問題を抱えており、彼の推奨する依存関係をインストールするとすべてが修正されました。
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>asm</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>