[JavaEE] Testing the Java EE Application : Basic Arquillian integration test

时间:2021-07-20 20:58:20

We have model like this:

 package com.pluralsight.bookstore.model;

 import javax.persistence.*;
import java.util.Date; /**
* @author Antonio Goncalves
* http://www.antoniogoncalves.org
* --
*/ @Entity
public class Book { // ======================================
// = Attributes =
// ====================================== @Id
@GeneratedValue
private Long id; @Column(length = 200)
private String title; @Column(length = 10000)
private String description; @Column(name = "unit_cost")
private Float unitCost; @Column(length = 50)
private String isbn; @Column(name = "publication_date")
@Temporal(TemporalType.DATE)
private Date publicationDate; @Column(name = "nb_of_pages")
private Integer nbOfPages; @Column(name = "image_url")
private String imageURL; @Enumerated
private com.pluralsight.bookstore.model.Language language; // ======================================
// = Constructors =
// ====================================== public Book() {
} public Book(String isbn, String title, Float unitCost, Integer nbOfPages, com.pluralsight.bookstore.model.Language language, Date publicationDate, String imageURL, String description) {
this.isbn = isbn;
this.title = title;
this.unitCost = unitCost;
this.nbOfPages = nbOfPages;
this.language = language;
this.publicationDate = publicationDate;
this.imageURL = imageURL;
this.description = description;
} // ======================================
// = Getters and Setters =
// ====================================== public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} public Float getUnitCost() {
return unitCost;
} public void setUnitCost(Float unitCost) {
this.unitCost = unitCost;
} public String getIsbn() {
return isbn;
} public void setIsbn(String isbn) {
this.isbn = isbn;
} public Date getPublicationDate() {
return publicationDate;
} public void setPublicationDate(Date publicationDate) {
this.publicationDate = publicationDate;
} public com.pluralsight.bookstore.model.Language getLanguage() {
return language;
} public void setLanguage(Language language) {
this.language = language;
} public Integer getNbOfPages() {
return nbOfPages;
} public void setNbOfPages(Integer nbOfPages) {
this.nbOfPages = nbOfPages;
} public String getImageURL() {
return imageURL;
} public void setImageURL(String imagURL) {
this.imageURL = imagURL;
} // ======================================
// = Methods hash, equals, toString =
// ====================================== @Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", description='" + description + '\'' +
", unitCost=" + unitCost +
", isbn='" + isbn + '\'' +
", publicationDate=" + publicationDate +
", language=" + language +
'}';
}
}

We have Resposity like this:

 package com.pluralsight.bookstore.repository;

 import com.pluralsight.bookstore.model.Book;

 import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
import java.util.List; import static javax.transaction.Transactional.TxType.REQUIRED;
import static javax.transaction.Transactional.TxType.SUPPORTS; /**
* @author Antonio Goncalves
* http://www.antoniogoncalves.org
* --
*/ // For readonly methods we want to using SUPPORTS
@Transactional(SUPPORTS)
public class BookRepository { // ======================================
// = Injection Points =
// ====================================== @PersistenceContext(unitName = "bookStorePU")
private EntityManager em; // ======================================
// = Business methods =
// ====================================== public Book find(Long id) {
return em.find(Book.class, id);
} public List<Book> findAll() {
// For complex SQL, we can also using Query language
TypedQuery<Book> query = em.createQuery("SELECT b FROM Book b ORDER BY b.title DESC", Book.class);
return query.getResultList();
} public Long countAll() {
TypedQuery<Long> query = em.createQuery("SELECT COUNT(b) FROM Book b", Long.class);
return query.getSingleResult();
} // For creating and deleting methods, we want to use REQUIRED
@Transactional(REQUIRED)
public Book create(Book book) {
em.persist(book);
return book;
} @Transactional(REQUIRED)
public void delete(Long id) {
em.remove(em.getReference(Book.class, id));
}
}

We want to create a integration test for BookResposity:

package com.pluralsight.bookstore.repository;

import com.pluralsight.bookstore.model.Book;
import com.pluralsight.bookstore.model.Language;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith; import javax.inject.Inject; import java.util.Date; import static org.junit.Assert.*; @RunWith(Arquillian.class)
public class BookRepositoryTest { @Inject
private BookRepository bookRepository; @Test
public void create() throws Exception {
// Test counting books
assertEquals(Long.valueOf(0), bookRepository.countAll());
assertEquals(0, bookRepository.findAll().size()); // Create book
Book book = new Book("isbn", "title", 12F, 123, Language.ENGLISH, new Date(), "imageURL", "description");
book = bookRepository.create(book); Long bookId = book.getId();
assertNotNull(bookId); // Find created book
Book bookFound = bookRepository.find(bookId);
assertEquals("title", bookFound.getTitle());
assertEquals(1, bookRepository.findAll().size()); // Delete the book
bookRepository.delete(bookId);
assertEquals(Long.valueOf(0), bookRepository.countAll());
assertEquals(0, bookRepository.findAll().size());
} @Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(BookRepository.class)
.addClass(Book.class)
.addClass(Language.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml");
} @org.junit.Test
public void create() {
}
}

@Deployment is part of test configuration, here we need to add all the dependices and test persistence.xml file.

    @Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(BookRepository.class)
.addClass(Book.class)
.addClass(Language.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml");
}