diff --git a/src/test/java/com/example/demo/controller/HelloControllerTest.java b/src/test/java/com/example/demo/controller/HelloControllerTest.java new file mode 100644 index 0000000..3861132 --- /dev/null +++ b/src/test/java/com/example/demo/controller/HelloControllerTest.java @@ -0,0 +1,38 @@ +package com.example.demo.controller; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.web.servlet.MockMvc; + +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; + +@WebMvcTest(HelloController.class) +class HelloControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + void hello_withoutName_returnsDefaultMessage() throws Exception { + mockMvc.perform(get("/hello")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello, World!")); + } + + @Test + void hello_withName_returnsPersonalizedMessage() throws Exception { + mockMvc.perform(get("/hello").param("name", "Spring")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello, Spring!")); + } + + @Test + void hello_withBlankName_returnsDefaultMessage() throws Exception { + mockMvc.perform(get("/hello").param("name", " ")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello, World!")); + } +}