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,不能插入重复值

 

插入前

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

 插入后

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

之前插入失败,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);
    }

插入前:

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

插入后:

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

在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 {
      List findByCategoryTypeIn(List 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 list = Arrays.asList(2,12,11);
        List 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层,所以还是得多测试才行啊。

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

 

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

    @Test
    public void findByCategoryTypeInTest(){
        List list = Arrays.asList(2,12,11);
        List 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群英汇

你可能感兴趣的:(Java)