This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class HelloService { | |
public String getHello() { | |
return "World"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
public class HelloServiceTest { | |
@Autowired | |
private HelloService helloService; | |
@Test | |
public void shouldReturnCorrectText() { | |
assertThat(helloService.getHello()).isEqualTo("World"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class SimpleController { | |
@RequestMapping("/hello") | |
@ResponseBody | |
public String hello() { | |
return "Hello!"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class SimpleController { | |
@Autowired | |
private HelloService helloService; | |
@RequestMapping("/hello") | |
@ResponseBody | |
public String hello() { | |
return "Hello!"; | |
} | |
@RequestMapping("/helloworld") | |
@ResponseBody | |
public String helloWorld() { | |
return "Hello " + helloService.getHello() + "!"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) #1 | |
@WebMvcTest #2 | |
public class SimpleControllerTest { | |
@Autowired | |
private MockMvc mockMvc; | |
@Test | |
public void shouldReturnExpectedText() throws Exception { | |
mockMvc | |
.perform(get("/hello")) #3 | |
.andExpect(status().isOk()) #4 | |
.andExpect(content().string(containsString("Hello!"))); #5 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) | |
@SpringBootTest #1 | |
@AutoConfigureMockMvc #2 | |
public class SimpleControllerTest { | |
@Autowired | |
private MockMvc mockMvc; | |
@Test | |
public void shouldReturnExpectedText() throws Exception { | |
mockMvc | |
.perform(get("/hello")) | |
.andDo(print()) | |
.andExpect(status().isOk()) | |
.andExpect(content().string(containsString("Hello!"))); | |
} | |
@Test | |
public void shouldReturnExpectedTextFromService() throws Exception { | |
mockMvc | |
.perform(get("/helloworld")) | |
.andDo(print()) | |
.andExpect(status().isOk()) | |
.andExpect(content().string(containsString("Hello World!"))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
@AutoConfigureMockMvc | |
public class SimpleControllerTest { | |
@Autowired | |
private MockMvc mockMvc; | |
@MockBean | |
private HelloService service; #1 | |
@Test | |
public void shouldReturnExpectedText() throws Exception { | |
mockMvc | |
.perform(get("/hello")) | |
.andDo(print()) | |
.andExpect(status().isOk()) | |
.andExpect(content().string(containsString("Hello!"))); | |
} | |
@Test | |
public void shouldReturnExpectedTextFromService() throws Exception { | |
when(service.getHello()).thenReturn("Mock"); #2 | |
mockMvc | |
.perform(get("/helloworld")) | |
.andDo(print()) | |
.andExpect(status().isOk()) | |
.andExpect(content().string(containsString("Hello Mock!"))); #3 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
verify(service, times(1)).getHello(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment