Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.springboot.testjunit;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- import org.springframework.test.web.servlet.MockMvc;
- import org.springframework.test.web.servlet.ResultActions;
- import org.springframework.test.web.servlet.setup.MockMvcBuilders;
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class SpringBootAppTestJUnitApplicationTests {
- @Autowired
- TestController testcontroller;
- private MockMvc mvc;
- @Before
- public void setup() {
- mvc = MockMvcBuilders.standaloneSetup(testcontroller).build();
- }
- @Test
- public void testGet() throws Exception {
- mvc.perform(get("/home")).andExpect(status().isOk()).andExpect(view().name("index"));
- }
- @Test
- public void testPost()throws Exception{
- ResultActions result = mvc.perform(post("/top").param("hello" ,"name")).andExpect(view().name("login"));
- result.andExpect(status().isOk());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement