[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, 其核心由一系列抽象的标准规范所组成, 是针对目前软件开发中所普遍面临问题的解决方案. 注意以上定义中的"抽象 ...
随机推荐
- 利用freemarker导出页面格式复杂的excel
刚开始大家可能会利用poi生成简单的excel,但是遇到需要生成复杂的excel,poi导出excel就比较困难,这时候可以利用freemarker来渲染实现实现生成复杂的excel, 首先,将exc ...
- HTML 5 <aside> 标签
定义和用法 <aside> 标签定义 article 以外的内容.aside 的内容应该与 article 的内容相关. 实例 <p>Me and my family visi ...
- POJ_1062_(dijkstra)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 48126 Accepted: 14343 Descripti ...
- python游戏开发:pygame事件与设备轮询
一.pygame事件 1.简介 pygame事件可以处理游戏中的各种事情.其实在前两节的博客中,我们已经使用过他们了.如下是pygame的完整事件列表: QUIT,ACTIVEEVENT,KEYDOW ...
- Jmeter之JDBC请求参数化(二)
二.上面已经讲了一些基本的配置,和简单的jdbc请求,下面来看下具体的如何将查询语句参数化. 参数化这里有几种方法,foreach,计数器,csv等,这里介绍几种方法.
- JavaScript中的方法
JavaScript中的方法 在JavaScript中,可以通过对象来调用对应的方法.在JavaScript中,有三个重要的window对象方法:用于显示警告信息的alert.用于显示确认信息的con ...
- Linux常用命令——帮助命令
1.帮助命令:man man 命令 获取指定命令的帮助 [dmtsai@study ~]$ man date DATE (1) User Commands DATE(1) #注意这个(1),代表的是m ...
- css--小白入门篇2
一.css基础选择器 html负责结构,css负责样式,js负责行为. css写在head标签里面,容器style标签. 先写选择器,然后写大括号,大括号里面是样式. 1 <style type ...
- http怎么做自动跳转https
Nginx版本 server { listen 80; server_name localhost; rewrite ^(.*)$ https://$host$1 permanent; ...
- session对象的使用
session对象的使用 制作人:全心全意 session在网络中被称为会话.由于HTTP协议是一种无状态协议,也就是当一个客户向服务器发出请求,服务器接收请求,并返回响应后,该连接就结束了,而服务器 ...