Created
August 20, 2021 03:47
-
-
Save pablohdzvizcarra/2b22e54d2c44dfda0df4ed6412ed63bd to your computer and use it in GitHub Desktop.
test about Spring controller
This file contains hidden or 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
package jvm.pablohdz.spring09testingcontrollers.controller; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.extension.ExtendWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | |
import org.springframework.test.context.junit.jupiter.SpringExtension; | |
import org.springframework.test.web.servlet.MockMvc; | |
import org.springframework.test.web.servlet.MvcResult; | |
import org.springframework.test.web.servlet.RequestBuilder; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | |
@ExtendWith(SpringExtension.class) | |
@WebMvcTest(HelloController.class) | |
class HelloControllerIntegrationTest { | |
@Autowired | |
private MockMvc mvc; | |
@Test | |
void hello() throws Exception { | |
RequestBuilder request = get("/hello"); | |
MvcResult result = mvc.perform(request).andReturn(); | |
assertEquals("Hello, World", result.getResponse().getContentAsString()); | |
} | |
@Test | |
void testHelloWithName() throws Exception { | |
mvc.perform(get("/hello?=name=Pablo")) | |
.andExpect(content().string("Hello, Pablo")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment