We want to connect the people who have knowledge to the people who need it, to bring together people with different perspectives so they can understand each other better, and to empower everyone to share their knowledge.
<span><span><<span>dependency</span>></span> <span><<span>groupId</span>></span>org.springframework.boot<span></<span>groupId</span>></span> <span><<span>artifactId</span>></span>spring-boot-starter-web<span></<span>artifactId</span>></span> <span><<span>version</span>></span>$</span><span>{spring.boot.version}</span><span><span></<span>version</span>></span> <span></<span>dependency</span>></span> <span><<span>dependency</span>></span> <span><<span>groupId</span>></span>org.springframework.boot<span></<span>groupId</span>></span> <span><<span>artifactId</span>></span>spring-boot-starter-test<span></<span>artifactId</span>></span> <span><<span>version</span>></span>$</span><span>{spring.boot.version}</span><span><span></<span>version</span>></span> <span><<span>scope</span>></span>test<span></<span>scope</span>></span> <span></<span>dependency</span>></span></span>
<spring.boot.version>2.7.0</spring.boot.version>
package com.example.spring_test.service; import org.springframework.stereotype.Service; @Service public class HelloService { public String hello(String who) { return "Hello " + who + '!'; } }
package com.example.spring_test.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.example.spring_test.service.HelloService; import lombok.AllArgsConstructor; @RestController @AllArgsConstructor public class HomeController { private final HelloService helloService; @GetMapping("/hello") public String hello(@RequestParam String who) { return helloService.hello(who); } }
package com.example.spring_test.test; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc; import com.example.spring_test.service.HelloService; @SpringBootTest @AutoConfigureMockMvc public class HomeControllerTest { @MockBean private HelloService helloService; @Autowired private MockMvc mockMvc; @Test public void helloTest() throws Exception { // given final String who = "World"; final String hello = "Hello World!"; when(helloService.hello(who)).thenReturn(hello); // when // then mockMvc.perform(get("/hello?who=World")) .andExpect(status().isOk()) .andExpect(content().string(hello)); verify(helloService, times(1)).hello(who); } }
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Input your email to receive reset password link, or if you remember your password, you can click