arquillian / arquillian-extension-persistence

Arquillian Database / Persistence Extension
Apache License 2.0
75 stars 68 forks source link

= Arquillian Persistence Extension :asciidoctor-source: https://raw.githubusercontent.com/arquillian/arquillian-extension-persistence/documentation/docs :numbered: :sectlink: :sectanchors: :sectid: :source-language: java :source-highlighter: coderay :sectnums: :icons: font :toc: left

image:https://travis-ci.org/arquillian/arquillian-extension-persistence.svg?branch=master["Build Status", link="https://travis-ci.org/arquillian/arquillian-extension-persistence"]

ifndef::generated-doc[] To read complete documentation visit http://arquillian.org/arquillian-extension-persistence/ endif::generated-doc[]

== Overview

[[what-is-this]] === What Is This?

Arquillian Persistence Extension was created to help you write tests where persistence layer is involved. Inspired by great framework called http://unitils.org/[Unitils], it brings a bunch of annotations to help you deal with the underlying data storage.

It comes with following features:

[[containers-used-for-testing]] === Containers Used For Testing

[[verified-with-following-databases]] === Verified With Following Databases

Enough talking, let's see it in action!

[[code-example]] === Code Example

[source,java]

@RunWith(Arquillian.class) public class UserPersistenceTest {

@Deployment public static Archive<?> createDeploymentPackage() { return ShrinkWrap.create(JavaArchive.class, "test.jar") .addPackage(UserAccount.class.getPackage()) .addPackages(true, "org.fest") // FEST Assert is not part of Arquillian .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") .addAsManifestResource("test-persistence.xml", "persistence.xml"); }

@PersistenceContext EntityManager em;

@Test @UsingDataSet("datasets/users.yml") @ShouldMatchDataSet("datasets/expected-users.yml") public void should_change_user_password() throws Exception { // given String expectedPassword = "LexLuthor"; UserAccount user = em.find(UserAccount.class, 2L);

  // when
  user.setPassword("LexLuthor");
  em.merge(user);

  // then
  assertThat(user.getPassword()).isEqualTo(expectedPassword);

} }

There are just two things which are different from the standard Arquillian test - @UsingDataSet and @ShouldMatchDataSet annotations. Former seeds the database using file in YAML format, and latter verifies database state using given file.

This example is taken from integration tests written for this project, so feel free to have a closer look.

But it's that easy! And there's more to come!

If you have any questions or would like to file feature request or bug report (hope not!) please have a look at http://arquillian.org/community/[the ways how you can get in touch with us].

ifdef::generated-doc[] [[guide]] == Guide

include::{asciidoctor-source}/maven-setup.adoc[] include::{asciidoctor-source}/transactional-tests.adoc[] include::{asciidoctor-source}/seeding-database.adoc[] include::{asciidoctor-source}/verifying-database-content.adoc[] include::{asciidoctor-source}/data-insert-strategies.adoc[] include::{asciidoctor-source}/cleaning-your-data.adoc[] include::{asciidoctor-source}/additional-configuration.adoc[]