When we create Entity and Respority, we also need to do validations to protect our data.

In Java, validations are built-in, using decorators. For Typescript, found a useful libaray to do the similar validation as well. Checkout class-validator

Entity:

@Entity
public class Book { // ======================================
// = Attributes =
// ====================================== @Id
@GeneratedValue
private Long id; @Column(length = 200)
@NotNull
@Size(min
= 1, max = 200)
private String title; @Column(length = 10000)
@Size(min = 1, max = 10000)
private String description; @Column(name = "unit_cost")
@Min(1)
private Float unitCost; @Column(length = 50)
@NotNull
@Size(min
= 1, max = 50)
private String isbn; @Column(name = "publication_date")
@Temporal(TemporalType.DATE)
@Past
private Date publicationDate; ....
}

Testing:

We want to test, if we give title as null, it should throw exception.

@RunWith(Arquillian.class)
public class BookRepositoryTest { @Inject
private BookRepository bookRepository; // We want the test throw exception
@Test(expected = Exception.class)
public void createInvalidBook() {
Book book = new Book("isbn", null, 12F, 123, Language.ENGLISH, new Date(), "imageURL", "description");
bookRepository.create(book);
} @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() {
}
}

Respority:

@Transactional(SUPPORTS)
public class BookRepository { // ======================================
// = Injection Points =
// ====================================== @PersistenceContext(unitName = "bookStorePU")
private EntityManager em; // ======================================
// = Business methods =
// ====================================== public Book find(@NotNull Long id) {
return em.find(Book.class, id);
} // For creating and deleting methods, we want to use REQUIRED
@Transactional(REQUIRED)
public Book create(@NotNull Book book) {
em.persist(book);
return book;
} }

Testing:

    @Test(expected = Exception.class)
public void findWithInvalidId() {
bookRepository.find(null);
}

最新文章

  1. 提高前端开发效率必备AngularJS (基础)
  2. Bootstrap~学习笔记索引
  3. linux系统启动时更改MAC地址
  4. 【leetcode】Distinct Subsequences(hard)
  5. 使用MyBatis Generator自动创建代码
  6. socket编程概述
  7. Android 线程模型
  8. 常用oracle查询总结
  9. BitmapFactory 加载图片到内存
  10. hive函数总结-字符串函数
  11. 推荐一本好书给即将走入工作的程序员and程序媴
  12. KeCode对照表(键盘按键的获取)
  13. 【剑指offer】删除字符也出现在一个字符串
  14. spark-RDD源码分析
  15. Android-SD卡相关操作
  16. 学习ActiveMQ(一):安装与启动
  17. H5 文本属性
  18. deepin使用笔记-解决蓝牙设备开机自动开启的问题
  19. Python Mock的入门(转)
  20. 国内淘宝镜像 cnpm转npm

热门文章

  1. Windows Azure中文博客 Windows Azure入门教学系列 (一): 创建第一个WebRole程序
  2. Cesium加载影像
  3. 关于defineProperty
  4. VMWare虚拟机中Ubuntu 16.04 (linux无桌面)配置静态IP上网
  5. HTML form without CSRF protection,HTML表单没有CSRF保护
  6. PKI中常用编码:ASN.1 DER BER Base64
  7. poi导出word时设置兼容性
  8. 【笔记JS/HTML/CSS】CSS3实现鼠标滑动显示动画(transition、transform)
  9. (转)淘淘商城系列——导入商品数据到索引库——Service层
  10. 安卓app测试之cpu监控