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");
}

[JavaEE] Testing the Java EE Application : Basic Arquillian integration test的更多相关文章

  1. JavaEE Tutorials (18) - Java EE平台安全介绍

    18.1Java EE安全概述278 18.1.1简单的应用安全演示279 18.1.2安全机制特性281 18.1.3应用安全特点28118.2安全机制282 18.2.1Java SE安全机制28 ...

  2. JavaEE Tutorials (27) - Java EE的并发工具

    27.1并发基础427 27.1.1线程和进程42827.2并发工具的主要组件42827.3并发和事务42927.4并发和安全43027.5jobs并发示例430 27.5.1运行jobs示例4302 ...

  3. JavaEE Tutorials (21) - Java EE安全:高级主题

    21.1使用数字证书331 21.1.1创建服务器证书332 21.1.2向证书安全域增加用户334 21.1.3为GlassFish服务器使用一个不同的服务器证书33421.2认证机制335 21. ...

  4. The differences between Java EE components and "standard" Java classes

    https://docs.oracle.com/javaee/7/tutorial/overview003.htm ava EE components are written in the Java ...

  5. Java EE (7) -- Java EE 6 Enterprise Architect Certified Master(1z0-807)

    Application Design Concepts and Principles Identify the effects of an object-oriented approach to sy ...

  6. Java EE (6) -- Java EE 5 Enterprise Architect Certified Master

    Section 1: Application Design Concepts and Principles Explain the main advantages of an object-orien ...

  7. Java EE (2) -- Java EE 6 Enterprise JavaBeans Developer Certified Expert(1z0-895)

    Introduction to Java EE Gain an understanding of the Java Platform, Enterprise Edition (Java EE) Exa ...

  8. Java EE 开发环境搭建

    1 Windows 1.1 JDK 下载: 下载地址:https://developer.oracle.com/java 安装文件:jdk-8u201-windows-x64.exe JDK 并不是越 ...

  9. 1. Java EE简介 - JavaEE基础系列

    什么是Java EE? 真的是你理解的那样吗? Java EE, 原名J2EE, 其核心由一系列抽象的标准规范所组成, 是针对目前软件开发中所普遍面临问题的解决方案. 注意以上定义中的"抽象 ...

随机推荐

  1. 构建一个.net的干货类库,以便于快速的开发 - 加密

    在开发程序的时候,加密是一个程序一个必须的功能,基本上任何程序都会用到加密,而不同的加密方式又适应不同需求,有些加密是不可逆的,最常见是用于用户密码的加密,因为很多时候程序里面不该显示出用户的明文密码 ...

  2. jquery中有关cookie的使用简要说明

    jquery.cookie.js 的配置 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. <script type="text/javascri ...

  3. 百度地图API在vue-cli中路径错误的问题

    在使用百度地图的时候,需要使用自定义的icon图片,百度的案例中使用的是线上地址,但当替换为本地图片路径的时候,错误出现了 这是本地图片地址 ) // 设置覆盖物大小 ); 这里有一点需要注意,这里路 ...

  4. go new() 和 make() 的区别

    看起来二者没有什么区别,都在堆上分配内存,但是它们的行为不同,适用于不同的类型. new(T) 为每个新的类型T分配一片内存,初始化为 0 并且返回类型为*T的内存地址:这种方法 返回一个指向类型为 ...

  5. S-HR之代码创建临时表并插入数据

    ... private String tempTab1 = null; //临时表EcirrWithPPTempTable public String getTempTable() { String ...

  6. Linux常用命令——帮助命令

    1.帮助命令:man man 命令 获取指定命令的帮助 [dmtsai@study ~]$ man date DATE (1) User Commands DATE(1) #注意这个(1),代表的是m ...

  7. enote笔记语言(3)(ver0.4)

    章节:enote笔记语言(3)     what&why(why not)&how&when&where&which:紫色,象征着神秘而又潜蕴着强大的力量,故取 ...

  8. 调用OpenSSL实现RSA加解密和签名操作

    调用OpenSSL实现RSA加解密和签名操作 RSA公钥可以从证书和公钥文件,RSA私钥可以从私钥文件中提取.OpenSSL使用了一种BIO抽象IO机制读写所用文件,可以打开文件相关联的BIO,通过B ...

  9. 基于element UI 的上传插件

    为了不再重复的上传文件,做了一个统一选择文件和上传文件的 基于 element UI :http://element-cn.eleme.io 前端实现文件下载和拖拽上传 演示 用法 <uploa ...

  10. mac下安装好jdk和jmeter后设置环境变量

    1. 执行vim ~/.bash_profile,打开文件: 2. 按i,进入输入状态,并输入如下信息,其中为jdk安装路径: export JAVA_HOME=/Library/Java/JavaV ...