Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Spring Boot] Problème test d intégration API REST

    3 mai 2022 à 15:00:32

    Bonjour la communauté,
    J'ai suivi le tuto de création d une API avec Spring Boot de bout en bout, ainsi que l implémentation d'un test (Lien exercice : https://openclassrooms.com/fr/courses/6900101-creez-une-application-java-avec-spring-boot/7078023-testez-votre-api-avec-spring-boot). Le 1er test marche (test d une réponse 200), mais le 2ème test échoue systématiquement (test que la valeur du prénom du 1er élément dans le liste Json est égale à "Laurent"). Quand je teste la méthode du contrôleur par un client HTTP externe comme Postman, ca marche pourtant bien, j'ai le contenu du Json qui est renvoyé, mais dans les méthodes de tests d intégration, la réponse HTTP contient un corps avec une liste Json Vide....Pouvez m'indiquer si j'aurais loupé quelque chose ? Merciii.

    Message d'Erreur :

    MockHttpServletRequest:

          HTTP Method = GET

          Request URI = /employees

           Parameters = {}

              Headers = []

                 Body = null

        Session Attrs = {}

    Handler:

                 Type = com.openclassrooms.api.controller.EmployeeController

               Method = com.openclassrooms.api.controller.EmployeeController#getEmployees()

    Async:

        Async started = false

         Async result = null

    Resolved Exception:

                 Type = null

    ModelAndView:

            View name = null

                 View = null

                Model = null

    FlashMap:

           Attributes = null

    MockHttpServletResponse:

               Status = 200

        Error message = null

              Headers = [Content-Type:"application/json"]

         Content type = application/json

                 Body = []

        Forwarded URL = null

       Redirected URL = null

              Cookies = []

    java.lang.AssertionError: No value at JSON path "$[0].firstName"

    at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:304)

    at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:73)

    at org.springframework.test.web.servlet.result.JsonPathResultMatchers.lambda$value$0(JsonPathResultMatchers.java:87)

    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:212)

    at com.openclassrooms.api.EmployeeApiIntegrationTests.testGetEmployees(EmployeeApiIntegrationTests.java:25)

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)

    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.base/java.lang.reflect.Method.invoke(Method.java:568)

    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)

    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)

    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)

    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)

    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)

    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)

    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)

    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)

    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)

    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)

    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)

    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)

    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)

    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)

    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)

    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)

    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)

    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)

    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)

    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)

    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)

    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)

    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)

    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)

    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)

    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)

    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)

    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)

    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)

    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

    Caused by: com.jayway.jsonpath.PathNotFoundException: No results for path: $[0]['firstName']

    -
    Edité par Nathan T. 3 mai 2022 à 16:23:11

    • Partager sur Facebook
    • Partager sur Twitter
      3 mai 2022 à 17:15:47

      Bonjour,

      Déplacement vers un forum plus approprié

      Le sujet est déplacé de la section Fonctionnement du site vers la section Langage Java

      • Partager sur Facebook
      • Partager sur Twitter
        23 mai 2022 à 23:43:32

        Bonjour, je rencontre également ce problème à ce jour, merci.
        • Partager sur Facebook
        • Partager sur Twitter

        [Spring Boot] Problème test d intégration API REST

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        • Editeur
        • Markdown