hooyantsing's Blog

第120次课程_Springboot_thymeleaf_mock

字数统计: 1.5k阅读时长: 8 min
2020/03/07

源辰76班

第120次课程

2020.03.07

内容

Springboot_thymeleaf_mock

项目路径:

**1.**Mock

用于测试,创建虚拟对象。

例如biz需要dao的对象,但dao还未开发完成。就可以mock出一个dao对象,来用于biz测试。

SpringBoot 使用的是:Mockito

User.java

[bean]

1
2
3
package com.yc.C76S3PlySpringBoot.bean;
public class User {
}

UserBiz.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.yc.C76S3PlySpringBoot.biz;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.yc.C76S3PlySpringBoot.bean.User;
import com.yc.C76S3PlySpringBoot.dao.UserMapper;

@Service
public class UserBiz {
     
     @Resource
     private UserMapper um;
     
     public boolean login(String name,String pwd) {
          User user = um.selectByNameAndPassword(name,  pwd);
          return user != null;
     }
}

UserMapper.java

由于还没完成,所以就一个接口。

1
2
3
4
5
package com.yc.C76S3PlySpringBoot.dao;
import com.yc.C76S3PlySpringBoot.bean.User;
public interface UserMapper {
     public User selectByNameAndPassword(String  name,String pwd);
}

C76S3PlySpringBootApplicationTests.java

@MockBean:标记需要创建的虚拟对象

存根验证:提前设置好每种输入值和对应的返回值。

行为验证:验证执行过程中是否调用过某个方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.yc.C76S3PlySpringBoot;

import javax.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.util.Assert;
import com.yc.C76S3PlySpringBoot.bean.User;
import com.yc.C76S3PlySpringBoot.biz.UserBiz;
import com.yc.C76S3PlySpringBoot.dao.TagMapper;
import com.yc.C76S3PlySpringBoot.dao.UserMapper;

@SpringBootTest
class C76S3PlySpringBootApplicationTests {
    
    @Resource
    private TagMapper tm;
    
    @Resource
    private UserBiz ubiz;
    
    @MockBean
    private UserMapper um;

    
    @Test
    void test1() {
        User user = new User();
        
        /**
         * 输入值的设置
         */
        Mockito.when(um.selectByNameAndPassword(Mockito.anyString(), Mockito.anyString())).thenReturn(null);
        
        /*
         * 存根验证
         */
        Mockito.when(um.selectByNameAndPassword("yc", "123")).thenReturn(user);
        Mockito.when(um.selectByNameAndPassword("zhangsan", "123")).thenReturn(null);
        Mockito.when(um.selectByNameAndPassword("lisi", "111")).thenReturn(user);
        
        boolean bool = ubiz.login("zhangsan", "123");
        Assert.isTrue(bool == false, "登录测试失败");
        
        bool  = ubiz.login("yc", "123");
        Assert.isTrue(bool, "登录测试失败");
        
        bool  = ubiz.login("lisi", "111");
        Assert.isTrue(bool, "登录测试失败3");
        
        bool  = ubiz.login("武松", "111");
        Assert.isTrue(bool == false, "登录测试失败4");
        
        bool  = ubiz.login("李逵", "111");
        Assert.isTrue(bool == false, "登录测试失败5");
        
        
        /**
         * 行为验证
         */
        Mockito.verify(um).selectByNameAndPassword("zhangsan", "123");
        Mockito.verify(um).selectByNameAndPassword("yc", "123");
        Mockito.verify(um).selectByNameAndPassword("lisi", "111");
        Mockito.verify(um).selectByNameAndPassword("武松", "111");
        Mockito.verify(um).selectByNameAndPassword("李逵", "111");
        
        //Mockito.verify(um).selectByNameAndPassword("林冲", "111");
    }
}

**2.**Thymeleaf

应用Thymeleaf技术,前提是springboot项目。

Spring EL表达式

.png

-1.png

Thymeleaf依赖,将以下代码加入到pom.xml

1
2
3
4
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.yml

从JSP迁移到Thymeleaf,由于Thymeleaf内部集成了视图解析器,因此不需要了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#server.port=80
#服务器端口:80
server:
  port: 80
spring:
#连接数据库配置  
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/c76-s3-ply-favorite
    username: root
    password: a
    driver-class-name: com.mysql.jdbc.Driver

#视图解析器:接收浏览器url,返回页面
#  mvc:
#   view:
  #    prefix: WEB-INF/page/
   #   suffix: .jsp

#mybatis
mybatis:
  #实体类别名 com.yc.C76S3PlySpringBoot.bean -> bean
  type-aliases-package: com.yc.C76S3PlySpringBoot.bean
  #驼峰命名法 类成员别名
  configuration:
    map-underscore-to-camel-case: true
#测试类配置  
logging:
  level:
    com:
      yc:
        C76S3PlySpringBoot:
          dao: debug
          web: warn

文件迁移

JSP技术相关

“webapp/“不再需要,内部的如web.xml和WEB-INF废止。

Thymeleaf技术相关

将资源新分配至src/main/resources

“static/“  <–  css js

“templates”  <–  html

index.html

从jstl迁移到Thymeleaf。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<html lang="en-US" xmlns:th="http://www.thymeleaf.org">
<head>
<title>搜藏</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript">
     function add() {
          window
                   .open(
                             'fav.do?op=toAdd',
                             'newwindow',
                             'height=300, width=400,  toolbar=no, menubar=no, scrollbars=no,  resizable=no,location=no, status=no');
     }
</script>
</head>
<body>
     <form name="favForm" method="post"
          action="/cang/fav.do;jsessionid=FF8C0487A99814B144B07E5633524A0F">
          <input type="hidden" name="op" value="toList" />
          <div class="banner" valign="top">搜藏</div>
          <table cellspacing="0" cellpadding="0"  border="0">
              <tr>
                   <td style="text-align: right;"  valign="top">
                        <!-- 左边Tag列表 -->
                        <div class="left_labels">
                             <table class="labels_table"  cellspacing="0" cellpadding="0"
                                  border="0">
                                  <tr>
                                      <td><a  href="toedit" style="font-weight: bold;">添加书签</a></td>
                                  </tr>
                                  <tr>
                                      <td  class="selected_label"><a
                                           href="index.s?flag=1">全部</a></td>
                                  </tr>
                                  <tr>
                                      <td><a  href="index.s?flag=2">未分类</a></td>
                                  </tr>
                                  <!-- <c:forEach  items="${tList}" var="tag"> -->
                                  <!-- th:text==null显示标签名,th:text!=null显示text内容并将标签名覆盖 -->
                                  <tr th:each="tag :  ${tList}">
                                      <td><a  th:text="${tag.tName}"  th:href="'index.s?tId='+${tag.tId}">标签名</a></td>
                                  </tr>
                                  <!-- </c:forEach> -->
                                  
                                  <tr>
                                      <td><a  style="font-weight: bold;" href="fav.do?op=toCloud">云图</a>
                                      </td>
                                  </tr>
                             </table>
                        </div>
                   </td>
                   <td>
                        <!-- 右边fav内容 -->
                        <div class="content_links">
                        
                             <!-- <c:set var="showFlist"  value="${empty fList ? showTag.favorites : fList  }"></c:set> -->
                        
                             <!-- <c:forEach  items="${showFlist }" var="fav"> -->
                             <div th:each="fav:${showTag  != null && showTag.favorites!=null ? showTag.favorites :  fList}" style="padding: 6px 10px;">
                                  <div>
                                      <a  th:text="${fav.fLabel }" th:href="'http://'+${fav.fUrl }"
                                           style="color:  blue; font-size: 18px;" target="_blank">网站名</a>
                                  </div>
                                  <div  th:text="${fav.fDesc }" style="color: black; font-size:  16px;">网站说明</div>
                                  <div th:text="${fav.fUrl  }" style="color: green; font-size: 14px;">网址
                                      </div>
                             </div>
                             <!-- </c:forEach> -->
                        </div>
                   </td>
              </tr>
          </table>
     </form>
</body>
</html>
CATALOG