MvcResult result = this.mockMvc.perform(
MockMvcRequestBuilders.get(mockUrl))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType("application/ json;charset=UTF-8"))
.andDo(MockMvcResultHandlers.print())
Donnez moi ce qui suit:
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 = []
Cependant, le tester avec
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"))
renvoie l'erreur suivante:
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)
Comment utiliser jsonPath?
Je pense que vous devez passer le Matcher à la méthode jsonPath
. Par exemple.
.andExpect(jsonPath("$.version", is("0.1")))
Votre code fonctionne très bien pour moi. J'utilise Jackson pour analyser les cas et c'est probablement la seule différence. Vérifiez mon code:
Classe de résultat:
public class Res {
String version;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
Manette:
@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);
}
Tester:
@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());
}
Réponse:
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 = []
Je ne peux que recommander d'essayer de changer la bibliothèque de l'analyseur Json que vous utilisez. Sinon, essayez de mettre à jour votre code avec toutes les pièces nécessaires pour créer un exemple simple, minimal et reproductible de votre problème.
Tout le mérite de cette réponse va à @pramodh (Caché dans les commentaires de OP)
J'ai eu exactement le même problème et l'installation de ses dépendances recommandées a tout corrigé.
<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>