SpringBoot控制層實現單元測試
阿新 • • 發佈:2019-05-06
scrip accept req mediatype exceptio for 需要 ont desc package com.springboot.demo.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
/**
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
/**
- @ClassName LeaveControllerTest
- @Description TODO
- @Author yunshuodeng
- @Date 2019-05-06 11:03
-
@Version 1.0br/>**/
@RunWith(SpringRunner.class)
public class LeaveControllerTest {
private MockMvc mockMvc;@Before
public void setUp() throws Exception{
mockMvc = MockMvcBuilders.standaloneSetup(new LeaveController()).build();
}@Test
public void hello() throws Exception{
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
說明:
new LeaveController()表示需要測試的控制器類
MockMvcRequestBuilders.get("/level/hello"):請求的方法
SpringBoot控制層實現單元測試