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. ASP.NET中的<%%>介绍

    一.主要用于ASP.NET前台绑定用的最多: <%#Eval("")%> <%#Bind("")%> <%=变量%> 1.& ...

  2. 对SNL语言的解释器实现尾递归优化

    对于SNL语言解释器的内容可以参考我的前一篇文章<使用antlr4及java实现snl语言的解释器>.此文只讲一下"尾递归优化"是如何实现的--"尾递归优化& ...

  3. 【转载】testlink 1.8.5 安装错误的解决方法

    TestLink所需环境为PHP+MYSQL (支持MS SQL等),系统推荐使用PHP5.2,安装成功以后,如果运行时出错,主要两种错: [1].HP Warning: strtotime(): I ...

  4. HDU_1072_Nightmare

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目描述:矩阵表示迷宫,0表示墙,1表示路,2表示起点,3表示终点,4表示重置炸弹时间(6秒),你需 ...

  5. 基于 CentOS 搭建Seafile个人网盘

    一.安装 Seafile 安装依赖环境使用 yum 安装 Python 及 MySQL: yum install python python-setuptools python-imaging pyt ...

  6. CAD控件:COM接口实现自定义实体

    1. 实现步骤: 3 1. 实现步骤: 参考例子 :Src\MxDraw5.2\samples\ie\iedemoTest.htm 1) 增加自定义实体对象 调用DrawCustomEntity函数, ...

  7. The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_60\bin;C:\Windows\Sun\Jav

    启动项目自动结束,查看日志发现 [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache To ...

  8. vivo手机执行input命令提示killed

    异常现象: 使用vivo手机时发现通过inputManager发送按键.执行屏幕滑动等动作失效,相关API并没有任何异常抛出,继续跟踪发现shell控制台执行input进行屏幕滑动.发送文本.模拟按键 ...

  9. C#读取文件-古文观止(总结一下)

    1,读取单个文件 //读取一个文本文件 private void buttonRead_Click(object sender, EventArgs e) { String path = Enviro ...

  10. Luogu P2052 [NOI2011]道路修建

    吐槽一下 我开了\(-O2\)优化结果跑的更慢了什么鬼???!!! 我怕不是吸了一口毒氧气 不要脸的放上我的博客,欢迎大家前来面基 题目大意 给定一棵有\(n\)个节点的树,树中有\({n-1}\)条 ...