6、买家类目Service层搭建

6、买家类目Service层搭建

(1)新建service包

然后定义一个CategoryService接口

在这里插入图片描述
代码编辑:

package com.yummy.sell.service;

import com.yummy.sell.dataobject.ProductCategory;

import java.util.List;

/**
 * @author LYM
 * @date 2021/3/3 16:17
 */
public interface CategoryService {

    ProductCategory findOne(Integer categoryId);

    List<ProductCategory> findAll();

    List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);

    ProductCategory save(ProductCategory productCategory);

}

(2)接着写一个实现类,在service包下面新建一个impl包,新建实现类

在这里插入图片描述

代码编辑:

package com.yummy.sell.service.impl;

import com.yummy.sell.dataobject.ProductCategory;
import com.yummy.sell.repository.ProductCategoryRepository;
import com.yummy.sell.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LYM
 * @date 2021/3/3 16:27
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private ProductCategoryRepository repository;
    @Override
    public ProductCategory findOne(Integer categoryId) {
        return repository.findOne (categoryId);
    }

    @Override
    public List<ProductCategory> findAll() {
        return repository.findAll ();
    }

    @Override
    public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
        return repository.findByCategoryTypeIn (categoryTypeList);
    }

    @Override
    public ProductCategory save(ProductCategory productCategory) {
        return repository.save (productCategory);
    }
}

(3)接着我们来创建单元测试

  • 首先进行第一个测试findOne方法测试

首部注入依赖:

@RunWith (SpringRunner.class)
@SpringBootTest

注入依赖:

    @Autowired
    private CategoryServiceImpl categoryService;

测试代码:

 @Test
    public void testFindOne() {
        ProductCategory productCategory=categoryService.findOne (1);
        Assert.assertEquals (new Integer (1),productCategory.getCategoryId ());
    }

运行测试:
在这里插入图片描述

  • testFindAll方法测试

    @Test
    public void testFindAll() {
        List<ProductCategory> productCategoryList=categoryService.findAll ();
        Assert.assertNotEquals (0,productCategoryList.size ());
    }
  • testFindByCategoryTypeIn方法测试
    @Test
    public void testFindByCategoryTypeIn() {
        List<ProductCategory> productCategoryList=categoryService.findByCategoryTypeIn (Arrays.asList (1,2,3,4));
        Assert.assertNotEquals (0,productCategoryList.size ());
    }
  • testSave方法测试
    public void testSave() {
        ProductCategory productCategory=new ProductCategory ("男生专项",11);
        ProductCategory result=categoryService.save (productCategory);
        Assert.assertNotNull (result);
    }
  • testSave方法测试
    @Test
    public void testSave() {
        ProductCategory productCategory=new ProductCategory ("男生专项",11);
        ProductCategory result=categoryService.save (productCategory);
        Assert.assertNotNull (result);
    }

这里我将所有方法全部测试一遍
在这里插入图片描述
由上图可知代码测试都通过了。我们更新一下数据库看看:
在这里插入图片描述

  • 单元测试总的代码
package com.yummy.sell.service.impl;

import com.yummy.sell.dataobject.ProductCategory;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;

/**
 * @author LYM
 * @date 2021/3/3 16:36
 */
@RunWith (SpringRunner.class)
@SpringBootTest
public class CategoryServiceImplTest extends TestCase {

    @Autowired
    private CategoryServiceImpl categoryService;

    @Test
    public void testFindOne() {
        ProductCategory productCategory=categoryService.findOne (1);
        Assert.assertEquals (new Integer (1),productCategory.getCategoryId ());
    }

    @Test
    public void testFindAll() {
        List<ProductCategory> productCategoryList=categoryService.findAll ();
        Assert.assertNotEquals (0,productCategoryList.size ());
    }

    @Test
    public void testFindByCategoryTypeIn() {
        List<ProductCategory> productCategoryList=categoryService.findByCategoryTypeIn (Arrays.asList (1,2,3,4));
        Assert.assertNotEquals (0,productCategoryList.size ());
    }

    @Test
    public void testSave() {
        ProductCategory productCategory=new ProductCategory ("男生专项",11);
        ProductCategory result=categoryService.save (productCategory);
        Assert.assertNotNull (result);
    }
}

买家类目的搭建基本上,后面会分享一下controller层搭建❤

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值