Skip to content

Instantly share code, notes, and snippets.

@xialeistudio
Created April 28, 2023 09:42
Show Gist options
  • Save xialeistudio/de18ac3f1c3ea1781cc42c272217998e to your computer and use it in GitHub Desktop.
Save xialeistudio/de18ac3f1c3ea1781cc42c272217998e to your computer and use it in GitHub Desktop.
SpringBootApplication with mock and unit test
package com.example.springbootjunitexample;
import com.example.springbootjunitexample.SpringbootJunitExampleApplication.GreetingService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.web.servlet.MockMvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
public class HttpRequestTest {
@Autowired
private MockMvc mockMvc;
@Test
public void greetingShouldReturnDefaultMessage() throws Exception {
mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello World")));
}
@MockBean
private GreetingService service;
@Test
public void greetingShouldReturnMessageFromService() throws Exception {
when(service.greeting()).thenReturn("Hello Mock");
mockMvc.perform(get("/greeting")).andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("Hello Mock")));
}
@Test
public void helloShouldReturnMessage() throws Exception {
mockMvc.perform(get("/hello")).andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.a", equalTo("b")));
}
}
package com.example.springbootjunitexample;
import java.util.HashMap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@SpringBootApplication
public class SpringbootJunitExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJunitExampleApplication.class, args);
}
@org.springframework.stereotype.Controller
static class Controller {
@RequestMapping("/")
@ResponseBody
public String greeting() {
return "Hello World";
}
}
@org.springframework.stereotype.Controller
static class GreetingController {
private final GreetingService service;
GreetingController(GreetingService service) {
this.service = service;
}
@GetMapping("/greeting")
@ResponseBody
public String greeting() {
return service.greeting();
}
@GetMapping("/hello")
@ResponseBody
public HashMap<String, String> hello() {
return new HashMap<String, String>() {{
put("a", "b");
}};
}
}
@Service
static class GreetingService {
public String greeting() {
return "Hello World";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment