Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablohdzvizcarra/2b22e54d2c44dfda0df4ed6412ed63bd to your computer and use it in GitHub Desktop.
Save pablohdzvizcarra/2b22e54d2c44dfda0df4ed6412ed63bd to your computer and use it in GitHub Desktop.
test about Spring controller
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