SpringBoot微信点餐系统—5、买家端的开发-买家类目(下)—测试、回顾

5.3.5测试

5.3.5.1单元测试加断言,用assertNotEquals方法

    public static void assertNotEquals(Object unexpected, Object actual) {
        assertNotEquals((String)null, unexpected, actual);
    }

再次测试是否能够插入得进去

    @Test
    public void saveTest(){
        //ProductCategory productCategory = repository.getOne(2);
        //ProductCategory productCategory = new ProductCategory();
        //productCategory.setCategoryId(2);
        //productCategory.setCategoryName("女生最爱");
        //productCategory.setCategoryType(3);
        //productCategory.setCategoryName("男生最爱");
        //productCategory.setCategoryType(4);
        //productCategory.setCategoryName("老少咸宜");
        //productCategory.setCategoryType(5);
        //productCategory.setCategoryType(12);
        ProductCategory productCategory = new ProductCategory("老少咸宜",12);
        ProductCategory result = repository.save(productCategory);//返回的也是ProductCategory这个对象
        //Assert.assertNotNull(result);//断言,判断是否成功,返回的result不等于null
        Assert.assertNotEquals(null,result);
    }

失败了。。。因为category_type有唯一约束键uqe_category_type,不能插入重复值

 

插入前

 插入后

之前插入失败,category_id也算增加了一次。。。。

5.3.5.2希望测试完数据库是干净的,不要有我们测试的数据的。 使用@Transactional标签

@Test
    @Transactional//事务
    public void saveTest(){
        //ProductCategory productCategory = repository.getOne(2);
        //ProductCategory productCategory = new ProductCategory();
        //productCategory.setCategoryId(2);
        //productCategory.setCategoryName("女生最爱");
        //productCategory.setCategoryType(3);
        //productCategory.setCategoryName("男生最爱");
        //productCategory.setCategoryType(4);
        //productCategory.setCategoryName("老少咸宜");
        //productCategory.setCategoryType(5);
        //productCategory.setCategoryType(12);
        ProductCategory productCategory = new ProductCategory("老少咸宜",10);
        ProductCategory result = repository.save(productCategory);//返回的也是ProductCategory这个对象
        //Assert.assertNotNull(result);//断言,判断是否成功,返回的result不等于null
        Assert.assertNotEquals(null,result);
    }

插入前:

插入后:

在Servcie里面添加@Transactional注解,如果Service方法里面有抛出异常的话,它会回滚,以前产生的数据会被删除,不会留在数据库里面。单元测试里面呢,这个事务完全就是你所做的事情做完之后都会被回滚。

 

 一次性查的,查商品列表的时候,先查商品,再查类目,肯定是一次性查的。而不是分很多次,而且我们是通过category_type类目编号来查的。categoryType的list来查类目。

package com.imooc.sell.repository;
import com.imooc.sell.dataobject.ProductCategory;//import org.springframework.data.domain.Example;import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
//import java.util.Locale;
//import java.util.Optional;
/**
 * Created by zhongzh
 * 2018-05-25 9:49
 * 主键是Integer类型的
 */public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
      List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);

}
package com.imooc.sell.repository;
import com.imooc.sell.dataobject.ProductCategory;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;//import org.springframework.boot.SpringApplication;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;
import javax.transaction.Transactional;import java.util.Arrays;import java.util.List;
//import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTestpublic class ProductCategoryRepositoryTest {
    @Autowired
    private ProductCategoryRepository repository;
    @Test
    public void findOneTest() {
        ProductCategory productCategory = repository.getOne(1);
        //ProductCategory productCategory = repository.findById(1);        System.out.println(productCategory.toString());
    }
    @Test
    @Transactional//事务
    public void saveTest(){
        //ProductCategory productCategory = repository.getOne(2);
        //ProductCategory productCategory = new ProductCategory();
        //productCategory.setCategoryId(2);
        //productCategory.setCategoryName("女生最爱");
        //productCategory.setCategoryType(3);
        //productCategory.setCategoryName("男生最爱");
        //productCategory.setCategoryType(4);
        //productCategory.setCategoryName("老少咸宜");
        //productCategory.setCategoryType(5);
        //productCategory.setCategoryType(12);
        ProductCategory productCategory = new ProductCategory("老少咸宜",10);
        ProductCategory result = repository.save(productCategory);//返回的也是ProductCategory这个对象
        //Assert.assertNotNull(result);//断言,判断是否成功,返回的result不等于null
        Assert.assertNotEquals(null,result);
    }
    @Test
    public void findByCategoryTypeInTest(){
        List<Integer> list = Arrays.asList(2,12,11);
        List<ProductCategory> result = repository.findByCategoryTypeIn(list);
        //集合的元素大于0就是成功了
        Assert.assertNotEquals(0
        ,result.size());
    }
}

测试一下,成功了

 

5.3.5.3因为查询的时候需要实体类对象有一个无参的构造方法才行。

package com.imooc.sell.dataobject;
//import javax.persistence.Table;

import lombok.Data;import lombok.Getter;import lombok.Setter;import lombok.ToString;import org.hibernate.annotations.DynamicUpdate;import org.hibernate.annotations.Proxy;
import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import java.util.Date;
/**
 * 类目
 * Created by zhongzh
 * 2018-05-20 9:31
 * s_product_category
 *///@Table(name = "s_product_category")
@Entity//把数据库映射成对象
@Proxy(lazy = false)
@DynamicUpdate //@Data//@Data包含生成getter、setter和toString()方法//@Getter//如果只是需要Getter那就引入Getter//@Setter//如果只是需要Setter那就引入Setter//@ToString//如果只是需要ToString那就引入ToStringpublic class ProductCategory{
     /** 类目id. */
     @Id//Id是主键,自增类型的。
     //@GeneratedValue//相当于调用了native策略
     //@GeneratedValue(strategy = GenerationType.AUTO)
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Integer categoryId;//字段名的命名方式也是一样的,把下划线改成为空。
     /** 类目名字. */
     private String categoryName;

     /** 类目编号. */
     private Integer categoryType;
     /** 创建时间. */
     private Date createTime;
     /** 修改时间. */
     private Date updateTime;
    //不要忘了Getter和Setter方法
    /*
    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public Integer getCategoryType() {
        return categoryType;
    }

    public void setCategoryType(Integer categoryType) {
        this.categoryType = categoryType;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    @Override
    public String toString() {
        return "ProductCategory{" +
                "categoryId=" + categoryId +
                ", categoryName='" + categoryName + '\'' +
                ", categoryType=" + categoryType +
                '}';
    }
    */

    public ProductCategory(String categoryName, Integer categoryType) {
        this.categoryName = categoryName;
        this.categoryType = categoryType;
    }

    public ProductCategory() {
        super();
    }
}

大家一定要注意单元测试,你看你测试就测出那么多问题。你如果不测的话到了Service层,所以还是得多测试才行啊。

 

    @Test
    public void findByCategoryTypeInTest(){
        List<Integer> list = Arrays.asList(2,12,11);
        List<ProductCategory> result = repository.findByCategoryTypeIn(list);
        //集合的元素大于0就是成功了
        Assert.assertNotEquals(0,result.size());
    }

5.3.6回顾一下我们做了哪些事情?

在pom.xml引入了MySQL和data-jpa的依赖。引入了之后在application.yml做数据库的配置。配置了之后我们创建了一个表的映射:com.imooc.sell.dataobject.ProductCategory。注解也讲过了。

然后写DAO层的代码:com.imooc.sell.repository.ProductCategoryRepository。写了之后进行单元测试:com.imooc.sell.repository.ProductCategoryRepositoryTest。

然后就是插件lombok,可以节省开发时间。

 

 

https://www.cnblogs.com/ZHONGZHENHUA/p/9094846.html

 

 

 

欢迎进群交流258897306或关注公众号“IT群英汇

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 博客管理系统 ### #Springboot ## 主要功能 * 系统用户,角色,权限增删改查,权限分配,权限配色 * 文件上传可自由选择本地存储,七牛云存储,阿里云存储 * 系统字典 * 配置网站基本信息,包括博客数据限制 * 查看系统关键操作的日志(可在系统后台自动定制需要监控的模板) * 在线新增数据库并直接生成 前,后台基本源码,放到源码相应录中重启tomcat可直接使用,预览 * 系统定时任务的新增改查 立即启动 暂停 恢复 ## 技术框架 * 核心框架:`SpringBoot` * 安全框架:`Apache Shiro 1.3.2` * 缓存框架:`Redis 4.0` * 搜索框架:`Lucene 7.1` * 任务调度:`quartz 2.3` * 持久层框架:`MyBatis 3` mybatisplus 2.1.4 * 数据库连接池:`Alibaba Druid 1.0.2` * 日志管理:`SLF4J 1.7`、`Log4j` * 前框架:`layui` * 后台模板:layuicms 2.0。 * 富文本:wangEditor ### 开发环境 建议开发者使用以下环境,这样避免版本带来的问题 * IDE:`eclipse`/`idea` * DB:`Mysql5.7` `Redis` * JDK:`JAVA 8` * WEB:Tomcat8 (采用springboot框架开发时,并没有用到额外的tomcat 用的框架自带的) # 运行环境 * WEB服务器:`Weblogic`、`Tomcat`、`WebSphere`、`JBoss`、`Jetty` 等 * 数据库服务器:`Mysql5.5+` * 操作系统:`Windows`、`Linux` (Linux 大小写特别敏感 特别要注意,还有Linux上没有微软雅黑字体,需要安装这个字体,用于生成验证码) #用户名:admin 密码:123456 #数据库文件:mysiteforme.sql #数据库配置文件:mysiteforme下的src/main/resources下的application.yml #启动文件:mysiteforme下的com.mysiteforme.admin下的MysiteformeApplication.java #注意:启动之前先启动redis # http://localhost:8080 管理员用户名:test 密码:1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值