Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. package com.springboot.testjunit;
  2.  
  3. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  4. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  5. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  6. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
  7.  
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.test.context.SpringBootTest;
  13. import org.springframework.test.context.junit4.SpringRunner;
  14. import org.springframework.test.web.servlet.MockMvc;
  15. import org.springframework.test.web.servlet.ResultActions;
  16. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  17.  
  18. @RunWith(SpringRunner.class)
  19. @SpringBootTest
  20. public class SpringBootAppTestJUnitApplicationTests {
  21.  
  22. @Autowired
  23. TestController testcontroller;
  24.  
  25. private MockMvc mvc;
  26.  
  27. @Before
  28. public void setup() {
  29. mvc = MockMvcBuilders.standaloneSetup(testcontroller).build();
  30. }
  31.  
  32. @Test
  33. public void testGet() throws Exception {
  34. mvc.perform(get("/home")).andExpect(status().isOk()).andExpect(view().name("index"));
  35. }
  36.  
  37. @Test
  38. public void testPost()throws Exception{
  39. ResultActions result = mvc.perform(post("/top").param("hello" ,"name")).andExpect(view().name("login"));
  40. result.andExpect(status().isOk());
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement