java @Cacheable只是变得虚假?

时间:2017-02-06 00:11:05

标签: java unit-testing java-ee-7

我遇到 @Cacheable 的问题,无法正常运行。 我只是得到虚假的价值观?我试图运行代码,每个测试都得到一个错误的值。似乎可缓存不会从false变为true。

有人可以帮助我,为什么我的代码继续 false

@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()));
    }
}

0 个答案:

没有答案