java@Cacheable刚刚变假?

java@Cacheable刚刚变假?,java,unit-testing,java-ee-7,Java,Unit Testing,Java Ee 7,我无法正确运行@Cacheable。 我只是得到了错误的值?如果您尝试运行代码,每个测试都会得到一个错误的值。看来cacheable并没有从false变为true 有人能帮帮我吗,为什么我的代码总是出错 @Entity @Cacheable(value = true) @Table(name = "Book") @NamedQuery(name = FIND_ALL_BOOKS, query = "select b from Book b") @SequenceGenerator(name =

我无法正确运行@Cacheable。 我只是得到了错误的值?如果您尝试运行代码,每个测试都会得到一个错误的值。看来cacheable并没有从false变为true

有人能帮帮我吗,为什么我的代码总是出错

@Entity
@Cacheable(value = true) 
@Table(name = "Book")
@NamedQuery(name = FIND_ALL_BOOKS, query = "select b from Book b")
@SequenceGenerator(name = "SEQ_BOOK", initialValue = 50)

public class Book {

    public static final String FIND_ALL_BOOKS = "book.findAllBooks";

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_BOOK")
    private int id;

    @NotNull
    @Min(1)
    private Integer quantity;
    @NotNull
    @AllItemTypes
    @Enumerated(EnumType.STRING)
    private ItemType itemType;
    @NotNull
    @Title
    private String title;
    @NotNull
    @Price
    private Float price;
    @NotNull
    @Description
    private String description;
    @NotNull
    @Isbn
    private String isbnNumber;
    @NotNull
    @InitDate
    private Date initDate;
    @NotNull(message = "Invalid illustration is set to null")
    private Boolean illustrations;
    @NotNull
    @PageNumber
    private Integer numberOfPages;
    @NotNull
    @Version
    @Min(1)
    private Integer version;
    @NotNull
    @Author
    private String author;
    @NotNull
    @Min(1519)
    private Integer year;

    public Book() {
    }

    public Book(Integer quantity, ItemType itemType, String title, Float price, String description, String isbnNumber, Date initDate, Boolean illustrations, Integer numberOfPages, Integer version, String author, Integer year) {
        this.quantity = quantity;
        this.itemType = itemType;
        this.title = title;
        this.price = price;
        this.description = description;
        this.isbnNumber = isbnNumber;
        this.initDate = initDate;
        this.illustrations = illustrations;
        this.numberOfPages = numberOfPages;
        this.version = version;
        this.author = author;
        this.year = year;
    }

    public static String getFindAllBooks() {
        return FIND_ALL_BOOKS;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public ItemType getItemType() {
        return itemType;
    }

    public void setItemType(ItemType itemType) {
        this.itemType = itemType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getIsbnNumber() {
        return isbnNumber;
    }

    public void setIsbnNumber(String isbnNumber) {
        this.isbnNumber = isbnNumber;
    }

    public Date getInitDate() {
        return initDate;
    }

    public void setInitDate(Date initDate) {
        this.initDate = initDate;
    }

    public Boolean getIllustrations() {
        return illustrations;
    }

    public void setIllustrations(Boolean illustrations) {
        this.illustrations = illustrations;
    }

    public Integer getNumberOfPages() {
        return numberOfPages;
    }

    public void setNumberOfPages(Integer numberOfPages) {
        this.numberOfPages = numberOfPages;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return "Book{" +
        "id=" + id +
        ", quantity=" + quantity +
        ", itemType=" + itemType +
        ", title='" + title + '\'' +
        ", price=" + price +
        ", description='" + description + '\'' +
        ", isbnNumber='" + isbnNumber + '\'' +
        ", initDate=" + initDate +
        ", illustrations=" + illustrations +
        ", numberOfPages=" + numberOfPages +
        ", version=" + version +
        ", author='" + author + '\'' +
        ", year=" + year +
         '}';
    }
}

    ----------------------------------------------------------
        <<TEST>>
    ----------------------------------------------------------

public interface Constants {
    String PERSISTENCE_UNIT_NAME = "TEST";
}

public abstract class AbstractPeristEntityClass {

    protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    protected EntityManager em;
    protected EntityTransaction tx;

    @Before
    public void initEntityManager() throws Exception {
        em = emf.createEntityManager();
        tx = em.getTransaction();
    }

    @After
    public void closeEntityManager() throws SQLException {
        if (em != null) em.close();
    }

    protected Long getRandomId() {
        return Math.abs(new Random().nextLong());
    }
}

public class Book_v2_IT extends AbstractPeristEntityClass {


    @Test
    public void shouldCheckThatCustomerIsCacheableEqualsTrue() throws Exception {
        Book newBook = new Book(1, ItemType.BOOK, "BFG", 199.0f, "BFG - big friendly giant, is a book about a nice giant that put dreams into children's houses.", "9788205337961", new Date("06/30/2016 18:00:00"), true, 224, 1, "Roald Dahl", 1982);

        tx.begin();
        em.persist(newBook);
        tx.commit();

        assertNotNull(newBook.getId());

        Cache cache = emf.getCache();
        assertTrue(cache.contains(Book.class, newBook.getId()));
        cache.evict(Book.class);
        assertFalse(cache.contains(Book.class, newBook.getId()));
    }
}
@实体
@可缓存(值=真)
@表(name=“Book”)
@NamedQuery(name=FIND_ALL_Book,query=“从Book b中选择b”)
@SequenceGenerator(name=“SEQ_BOOK”,initialValue=50)
公共课堂用书{
公共静态最终字符串FIND\u ALL\u BOOKS=“book.findAllBooks”;
@身份证
@GeneratedValue(策略=GenerationType.SEQUENCE,generator=“SEQ_BOOK”)
私有int-id;
@NotNull
@最低(1)
私有整数数量;
@NotNull
@AllItemTypes
@枚举(EnumType.STRING)
私有ItemType ItemType;
@NotNull
@头衔
私有字符串标题;
@NotNull
@价格
私人浮动价格;
@NotNull
@描述
私有字符串描述;
@NotNull
@Isbn
私有字符串isbnNumber;
@NotNull
@起始日期
私人日期起始日期;
@NotNull(消息=“无效插图设置为null”)
私有布尔图;
@NotNull
@页码
私有整数页数;
@NotNull
@版本
@最低(1)
私有整数版本;
@NotNull
@作者
私有字符串作者;
@NotNull
@明(1519)
私人整数年;
公共书籍(){
}
公共图书(整数数量、项目类型、字符串标题、浮动价格、字符串描述、字符串isbnNumber、日期initDate、布尔插图、整数页数、整数版本、字符串作者、整数年){
这个。数量=数量;
this.itemType=itemType;
this.title=标题;
这个价格=价格;
this.description=描述;
this.isbnNumber=isbnNumber;
this.initDate=initDate;
这个。插图=插图;
this.numberOfPages=numberOfPages;
this.version=版本;
this.author=作者;
今年=年;
}
公共静态字符串getFindAllBooks(){
归还所有书籍;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共整数getQuantity(){
退货数量;
}
公共无效设置数量(整数数量){
这个。数量=数量;
}
public ItemType getItemType(){
返回项目类型;
}
公共void setItemType(ItemType ItemType){
this.itemType=itemType;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公开发行价格(){
退货价格;
}
公共定价(浮动价格){
这个价格=价格;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共字符串getIsbnNumber(){
返回isbnNumber;
}
public void setIsbnNumber(字符串isbnNumber){
this.isbnNumber=isbnNumber;
}
公共日期getInitDate(){
返回起始日期;
}
public void setInitDate(日期initDate){
this.initDate=initDate;
}
公共布尔值getIllustrations(){
返回插图;
}
公共插图(布尔插图){
这个。插图=插图;
}
公共整数getNumberOfPages(){
返回页数;
}
public void setNumberOfPages(整数numberOfPages){
this.numberOfPages=numberOfPages;
}
公共整数getVersion(){
返回版本;
}
公共void setVersion(整数版本){
this.version=版本;
}
公共字符串getAuthor(){
返回作者;
}
公共void setAuthor(字符串编写器){
this.author=作者;
}
公共整数getYear(){
回归年;
}
公共无效设置年(整数年){
今年=年;
}
@凌驾
公共字符串toString(){
返回“Book{”+
“id=”+id+
“,数量=”+数量+
“,itemType=“+itemType”+
“,title='”+title+'\''+
“,price=“+价格+
,description=''+description+'\''+
“,isbnNumber='”+isbnNumber+'\''+
“,initDate=“+initDate”+
“,插图=“+插图+
“,numberOfPages=“+numberOfPages+
“,version=“+版本+
“,author='”+author+'\''+
“,year=“+year”+
'}';
}
}
----------------------------------------------------------
----------------------------------------------------------
公共接口常数{
字符串持久性\u单元\u NAME=“测试”;
}
公共抽象类AbstractPeristentyClass{
受保护的静态EntityManagerFactory emf=Persistence.createEntityManagerFactory(Persistence\u UNIT\u NAME);
受保护的实体管理器em;
受保护实体事务tx;
@以前
public void initEntityManager()引发异常{
em=emf.createEntityManager();
tx=em.getTransaction();
}
@之后
public void closeEntityManager()引发SQLException{
如果(em!=null)em.close();
}
受保护的长getRandomId(){
返回Math.abs(newrandom().nextLong());
}
}
公共类书籍\u v2\u它扩展了AbstractPeristentyClass{
@试验
public void应检查CustomerisCachableEqualsTrue()是否引发异常{
Book newBook=新书(1,ItemType.Book,“BFG”,199.0f,“BFG-友好的大巨人,是一本关于一个美丽的巨人将梦想带入儿童之家的书。”,“9788205337961”,新日期(“06/30/2016 18:00:00”),真实,224,1,“Roald Dahl”,1982);
tx.begin();