[JavaEE] Testing the Java EE Application : Basic Arquillian integration test
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的更多相关文章
- 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 ...
- 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 ...
- JavaEE Tutorials (21) - Java EE安全:高级主题
21.1使用数字证书331 21.1.1创建服务器证书332 21.1.2向证书安全域增加用户334 21.1.3为GlassFish服务器使用一个不同的服务器证书33421.2认证机制335 21. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Java EE 开发环境搭建
1 Windows 1.1 JDK 下载: 下载地址:https://developer.oracle.com/java 安装文件:jdk-8u201-windows-x64.exe JDK 并不是越 ...
- 1. Java EE简介 - JavaEE基础系列
什么是Java EE? 真的是你理解的那样吗? Java EE, 原名J2EE, 其核心由一系列抽象的标准规范所组成, 是针对目前软件开发中所普遍面临问题的解决方案. 注意以上定义中的"抽象 ...
随机推荐
- datagrid 选中某行,翻页再翻回来,发现选中的行没有选中
不管有没有设置复选框,其实都是一样的,都是idField属性没有设置,加上去即可. $(function(){ $('#dg').datagrid({ url:'ContactServlet', to ...
- Set,Map与Array,Object对比
Map与Array 数据结构横向对比,用Map和Array分别实现最基本的增删改查: //增 { let theMap=new Map(); let theArray=[]; theMap.set(' ...
- ubuntu下删除和新建用户(并有su权限)
http://blog.csdn.net/speedme/article/details/8206144ubuntu下删除和新建用户(并有su权限) 如何创建ubuntu新用户?输入:sudo add ...
- C语言特殊知识点解析
1 数组 1.1 概念 数组是指某种数据类型,在内存上按照顺序存储.中括号([ ])是数组的标识,中括号内的数值标识该种数据类型变量的个数,中括号也有取值的作用. 1.2 数组使用 int a[10] ...
- git 删除分支如何恢复
强制删除了一个分支而后又想重新使用这个分支,该怎么找回该分支上的代码呢? 一:问题描述: 今天师父说上线几个功能,让我把开发的分支推送到远程.当打开git就傻眼了,之前开发好的分支被我删除了,就连推送 ...
- IOS7 状态栏和 Navigation Bar重叠的问题解决
一 Status bar重叠问题: 方法一:隐藏Status bar 在plist里面增加2个变量 Status bar is initially hidden -> YES Vie ...
- 07Java Server Pages
Java Server Pages JSP是一种Java servlet,主要用于实现Java web应用程序的用户界面部分. JSP全称Java Server Pages,是一种动态网页开发技术.它 ...
- BigDecimal运算
BigDecimal由任意精度整数未缩放值和32位整数级别组成 . 如果为零或正数,则刻度是小数点右侧的位数. 如果是负数,则数字的非标定值乘以10,以达到等级的否定的幂. 因此,BigDecimal ...
- C++ Primer(第4版)-学习笔记-第5部分:高级主题
第17章 用于大型程序的工具 异常处理 不存在数组或函数类型的异常.相反,如果抛出一个数组,被抛出的对象转换为指向数组首元素的指针,类似地,如果抛出一个函数,函数被转换为指向该函数的指针. 不要抛出 ...
- Network----轮询
轮询: 定时每隔多长时间刷新一次,但是,7X24的对服务器的压力会过大,因为在夜间或者是流量低峰期时,他还要持续工作. 客户端发一次请求,服务器就要相应一次. 长轮询: 和轮询的模式不同,长轮询是一次 ...