H__D  

  本章介绍使用 Junit4 测试 SpringBoot,关于 Junit4 的基本使用 参考:【Junit】Junit快速入门

  关于SpringMVC项目搭建参考,参考:【Maven】Eclipse 使用Maven创建SpringMVC Web项目

一、项目框架

1、新建一个SpringMVC项目

2、引入Junit4依赖,项目完整依赖如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>org.example</groupId>
 8     <artifactId>test-junit-springmvc</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <properties>
12         <!-- spring -->
13         <spring.version>5.2.4.RELEASE</spring.version>
14         <!-- Servlet -->
15         <servlet.version>3.0.1</servlet.version>
16         <jsp-api.version>2.2</jsp-api.version>
17         <!-- jstl -->
18         <jstl.version>1.2</jstl.version>
19         <standard.version>1.1.2</standard.version>
20 
21         <maven.compiler.source>8</maven.compiler.source>
22         <maven.compiler.target>8</maven.compiler.target>
23     </properties>
24 
25     <dependencies>
26         <!-- Spring -->
27         <dependency>
28             <groupId>org.springframework</groupId>
29             <artifactId>spring-webmvc</artifactId>
30             <version>${spring.version}</version>
31         </dependency>
32 
33         <!-- Spring Test 模块 -->
34         <dependency>
35             <groupId>org.springframework</groupId>
36             <artifactId>spring-test</artifactId>
37             <version>${spring.version}</version>
38             <scope>test</scope>
39         </dependency>
40 
41 
42         <!-- Servlet -->
43         <dependency>
44             <groupId>javax.servlet</groupId>
45             <artifactId>javax.servlet-api</artifactId>
46             <version>${servlet.version}</version>
47             <scope>provided</scope>
48         </dependency>
49         <dependency>
50             <groupId>javax.servlet.jsp</groupId>
51             <artifactId>jsp-api</artifactId>
52             <version>${jsp-api.version}</version>
53             <scope>provided</scope>
54         </dependency>
55 
56         <!-- jstl -->
57         <dependency>
58             <groupId>javax.servlet</groupId>
59             <artifactId>jstl</artifactId>
60             <version>${jstl.version}</version>
61         </dependency>
62         <dependency>
63             <groupId>taglibs</groupId>
64             <artifactId>standard</artifactId>
65             <version>${standard.version}</version>
66         </dependency>
67 
68         <!-- JSON -->
69         <dependency>
70             <groupId>com.fasterxml.jackson.core</groupId>
71             <artifactId>jackson-databind</artifactId>
72             <version>2.9.3</version>
73         </dependency>
74 
75         <dependency>
76             <groupId>junit</groupId>
77             <artifactId>junit</artifactId>
78             <version>RELEASE</version>
79             <scope>test</scope>
80         </dependency>
81     </dependencies>
82 
83 </project>  

3、项目controller如下:

 1 package com.test.junit.mvc.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.ResponseBody;
 6 import org.springframework.web.servlet.ModelAndView;
 7 
 8 import javax.servlet.http.HttpServletRequest;
 9 import java.util.HashMap;
10 import java.util.Map;
11 
12 @Controller
13 public class HelloController {
14 
15     @RequestMapping("/hello")
16     @ResponseBody
17     public String hello(){
18         return "Hello World";
19     }
20 
21     @RequestMapping("/map")
22     @ResponseBody
23     public Map map(){
24         Map<String, String> map = new HashMap<>();
25         map.put("name", "xiaoming");
26         map.put("age", "18");
27         return map;
28     }
29 
30     @RequestMapping(value="/getView")
31     public ModelAndView getView(HttpServletRequest request){
32         System.out.println("getView");
33         ModelAndView modelAndView = new ModelAndView("test-jsp");
34         return modelAndView;
35     }
36 
37 }
View Code

4、编辑测试类

 1 package com.test.junit.mvc;
 2 
 3 
 4 import org.junit.Before;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.http.MediaType;
 9 import org.springframework.test.context.ContextConfiguration;
10 import org.springframework.test.context.junit4.SpringRunner;
11 import org.springframework.test.context.web.WebAppConfiguration;
12 import org.springframework.test.web.servlet.MockMvc;
13 import org.springframework.test.web.servlet.MvcResult;
14 import org.springframework.test.web.servlet.ResultActions;
15 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
16 import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
17 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
18 import org.springframework.web.context.WebApplicationContext;
19 import org.springframework.web.servlet.ModelAndView;
20 
21 
22 // SpringRunner 继承了SpringJUnit4ClassRunner,
23 // 没有扩展任何功能;使用SpringRunner,名字简短而已
24 @RunWith(SpringRunner.class)
25 // @RunWith(SpringJUnit4ClassRunner.class)
26 // Web项目配置
27 @WebAppConfiguration
28 // 上下文配置
29 @ContextConfiguration(locations = {"classpath:spring/spring-mvc.xml", "classpath:spring/spring-context.xml"})
30 public class TestMVC {
31 
32     @Autowired
33     private WebApplicationContext webApplicationContext;
34 
35     private MockMvc mockMvc;
36 
37     @Before
38     public void setup() {
39         // 构建mockMvc 对象
40         this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
41     }
42 
43 
44     @Test
45     public void getView() throws Exception {
46         ResultActions resultActions = this.mockMvc.perform(
47                 // 数据的格式请求的url,请求的方法是get
48                 MockMvcRequestBuilders.get("/getView")
49         );
50         // 打印调试结果到控制台
51         resultActions.andDo(MockMvcResultHandlers.print());
52         // 获取返回结果
53         MvcResult mvcResult = resultActions.andReturn();
54         // 获取视图
55         ModelAndView modelAndView = mvcResult.getModelAndView();
56         System.out.println(modelAndView);
57     }
58 
59     @Test
60     public void map() throws Exception {
61         ResultActions resultActions = this.mockMvc.perform(
62                 // 数据的格式请求的url,请求的方法是get
63                 MockMvcRequestBuilders.get("/map")
64                         .contentType(MediaType.APPLICATION_JSON)
65         );
66         resultActions.andDo(MockMvcResultHandlers.print());
67         MvcResult mvcResult = resultActions.andReturn();
68         System.out.println(mvcResult.getResponse().getContentAsString());
69     }
70 
71     @Test
72     public void hello() throws Exception {
73         ResultActions resultActions = this.mockMvc.perform(
74                 // 数据的格式请求的url,请求的方法是get
75                 MockMvcRequestBuilders.get("/hello"));
76         // 打印调试结果到控制台
77         resultActions.andDo(MockMvcResultHandlers.print());
78         // 获取返回结果
79         MvcResult mvcResult = resultActions.andReturn();
80         System.out.println(mvcResult.getResponse().getContentAsString());
81     }
82 }

  关于 MockMvc 的使用

  参考:https://www.cnblogs.com/zengls/p/11316454.html

  参考:https://zhuanlan.zhihu.com/p/98074553

 

posted on 2021-04-27 01:44  H__D  阅读(493)  评论(0编辑  收藏  举报