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层搭建❤