原始访问层代码
package com.example.demo.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author:
* @Date: 2018/12/19 09:33
* @Description:
*/
@RestController
public class test1Controller {
@RequestMapping("/testDemo")
public String test1(){
System.out.println("这是第一个测试");
return "这是一个测试测试";
}
}
单元测试案例(1)
package com.example.demo.web;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.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;
/**
* @Author:
* @Date: 2018/12/19 09:44
* @Description:
*/
@SpringBootTest()
@RunWith(SpringRunner.class)
public class test1ControllerTest {
private MockMvc mvc;
@Before
public void setUp() {
mvc = MockMvcBuilders.standaloneSetup(new test1Controller()).build();
}
@Test
public void test1() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/testDemo"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
运行结果(1)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.1.RELEASE)
2018-12-19 10:36:01.912 INFO 2668 --- [ main] c.example.demo.web.test1ControllerTest : Starting test1ControllerTest on M7072XFZ with PID 2668 (started by maokai in F:\learnProject\demo)
2018-12-19 10:36:01.914 INFO 2668 --- [ main] c.example.demo.web.test1ControllerTest : No active profile set, falling back to default profiles: default
2018-12-19 10:36:03.285 INFO 2668 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-19 10:36:03.575 INFO 2668 --- [ main] c.example.demo.web.test1ControllerTest : Started test1ControllerTest in 2.076 seconds (JVM running for 2.871)
2018-12-19 10:36:03.844 INFO 2668 --- [ main] o.s.mock.web.MockServletContext : Initializing Spring TestDispatcherServlet ''
2018-12-19 10:36:03.845 INFO 2668 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Initializing Servlet ''
2018-12-19 10:36:03.845 INFO 2668 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Completed initialization in 0 ms
这是第一个测试
MockHttpServletRequest:
HTTP Method = GET
Request URI = /testDemo
Parameters = {}
Headers = {}
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type =