运行findByCategory_typeInTest测试方法的时候报错

来源:4-2 买家类目-dao(下)

IT菜鸟123

2018-12-21

报错信息

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productCategoryRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property category found for type ProductCategory!

ProductCategory实体类

package com.imooc.sell.dataobject;

import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
 * 类目
 */
@Entity
@DynamicUpdate
@lombok.Data
public class ProductCategory {
    //类目id
    @Id
    @GeneratedValue
    private Integer category_id;
    //类目名字
    private String category_name;
    //类目编号
    private Integer category_type;
//    //创建时间
////    private Date create_time;
////    //修改时间
////    private Date update_time;

    public ProductCategory() {
    }

    public ProductCategory(String category_name, Integer category_type) {
        this.category_name = category_name;
        this.category_type = category_type;
    }
}

ProductCategoryRepository接口

package com.imooc.sell.repository;

import com.imooc.sell.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer>{
List<ProductCategory> findByCategory_typeIn(List<Integer> categoryTypeList);
}

ProductCategoryRepositoryTest测试类

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.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;


@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {
    @Autowired
    private ProductCategoryRepository repository;

    @Test
    public void findOneTest() {
        ProductCategory productCategory = repository.findOne(1);
        System.out.println(productCategory.toString());
    }

    @Test
    @Transactional
    public void saveTest() {
        ProductCategory productCategory = new ProductCategory("女生最爱", 4);
        ProductCategory result = repository.save(productCategory);
        Assert.assertNotNull(result);
    }

    @Test
    public void findByCategory_typeInTest() {
        List<Integer> list = Arrays.asList(2,3,4);
       List<ProductCategory> result= repository.findByCategory_typeIn(list);
        Assert.assertNotEquals(0,result.size());
    }
}
写回答

1回答

廖师兄

2018-12-22

findByCategory_typeIn 方法名怎么会有下划线呢,需遵循驼峰命名,请参照视频和源码

0
2
廖师兄
回复
IT菜鸟123
private Integer category_type; 属性名也要改,你看我源码里,都是驼峰
2018-12-24
共2条回复

Spring Boot双版本(1.5/2.1) 打造企业级微信点餐系统

从0到1开发中小型企业级Java应用,并学会迭代重构技巧

6410 学习 · 5247 问题

查看课程