Skip to content

Latest commit

 

History

History
173 lines (143 loc) · 8.26 KB

File metadata and controls

173 lines (143 loc) · 8.26 KB

Related Resources and Integration Testing

Working with Relationships in Spring Data REST

Integration Testing in Spring

  • Spring test dependencies:

    springtestdep

  • Spring MVC Test Configuration

    • Enable Spring in Tests with JUnit 5

    junut5test

    • The WebApplicationContext Object
      @Autowired private WebApplicationContext webApplicationContext;
    • Mocking Web Context Beans

    ContextBeans

    • Verify Test Configuration
        @Test
        public void givenWac_whenServletContext_thenItProvidesGreetController() {
             ServletContext servletContext = webApplicationContext.getServletContext();
             Assert.assertNotNull(servletContext);
             Assert.assertTrue(servletContext instanceof MockServletContext);
             Assert.assertNotNull(webApplicationContext.getBean("greetController"));
             }
      
  • Writing Integration Tests

    • Verify View Name
        http://localhost:8080/spring-mvc-test/
      
        @Test
        public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() {
        this.mockMvc.perform(get("/homePage")).andDo(print())
       .andExpect(view().name("index"));
       }
      
    • Verify Response Body
      • We'll invoke the /greet endpoint from our test as:

          http://localhost:8080/spring-mvc-test/greet
        
      • The expected output will be:

          {
          "id": 1,
          "message": "Hello World!!!"
          }
        
      • Let's see the test code:

          @Test
          public void givenGreetURI_whenMockMVC_thenVerifyResponse() {
          MvcResult mvcResult = this.mockMvc.perform(get("/greet"))
          .andDo(print()).andExpect(status().isOk())
          .andExpect(jsonPath("$.message").value("Hello World!!!"))
          .andReturn();
        
          Assert.assertEquals("application/json;charset=UTF-8", 
           mvcResult.getResponse().getContentType());
              }
        
    • Send GET Request With Path Variable
      • invoke the /greetWithPathVariable/{name} endpoint from our test as:

          http://localhost:8080/spring-mvc-test/greetWithPathVariable/John
        
      • The expected output will be:

          {
              "id": 1,
              "message": "Hello World John!!!"
          }
        
      • Let's see the test code:

          @Test
          public void givenGreetURIWithPathVariable_whenMockMVC_thenResponseOK() {
              this.mockMvc
              .perform(get("/greetWithPathVariable/{name}", "John"))
              .andDo(print()).andExpect(status().isOk())
              
              .andExpect(content().contentType("application/json;charset=UTF-8"))
              .andExpect(jsonPath("$.message").value("Hello World John!!!"));
          }
        
    • Send GET Request With Query Parameters
      • invoke the /greetWithQueryVariable?name={name} endpoint from our test as:

          http://localhost:8080/spring-mvc-test/greetWithQueryVariable?name=John%20Doe
        
      • the expected output will be:

          {
              "id": 1,
              "message": "Hello World John Doe!!!"
          }
        
      • the test code:

         @Test
         public void givenGreetURIWithQueryParameter_whenMockMVC_thenResponseOK() {
             this.mockMvc.perform(get("/greetWithQueryVariable")
             .param("name", "John Doe")).andDo(print()).andExpect(status().isOk())
             .andExpect(content().contentType("application/json;charset=UTF-8"))
             .andExpect(jsonPath("$.message").value("Hello World John Doe!!!"));
         }
        
    • Send POST Request
    • invoke the /greetWithPost endpoint from our test as:

       http://localhost:8080/spring-mvc-test/greetWithPost
      
    • output:

       {
           "id": 1,
           "message": "Hello World!!!"
       }
      
    • And test code is:

            @Test
            public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() {
                this.mockMvc.perform(post("/greetWithPost")).andDo(print())
                .andExpect(status().isOk()).andExpect(content()
                .contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$.message").value("Hello World!!!"));
            }