Blog

Monday, 18 November 2013

Arquillian Persistence+REST+JRebel

Wondering how to speed up integration test development for your RESTful service?

Awesome Arquillian has just got new REST extension. Now we can test our REST services so easily. There is however a little inconvenience.
IMHO good integration test integrates from public API down to DB, so in case of REST service we should be able to test our app by invoking REST endpoints and not be forced to mock DAOs.

REST+Persistence

Luckily thanks to +Bartosz Majsak we have Arquillian Persistence extension that allows us to setup database before each test. The only problem is that it must run inside container and REST extension works in @RunAsClient mode. There is an issue ARQ-1077 to make Persistence extension work in client mode but it's not resolved yet. However there is a workaround for that.

In general you make the deployment testable
    @Deployment(testable = true)
    public static WebArchive create()
Then you add test that runs in container and make it run first with @InSequence annotation.
    @UsingDataSet
    @Test
    @InSequence(1)
    public void setupDB_ARQ1077_Workaround()
    {

    }

That test should be decorated with @UsingDataSet annotation that triggers database update (with proper dataset).
Then you write your REST tests that run in client mode and have @InSequence param bigger than 1.
    @RunAsClient
    @InSequence(2)
    @Test
    public void createPackage(@ArquillianResteasyResource("api") PackageResource packageResource)

Notice that all REST tests can have same @InSequence value.

Faster turnaround

Once you get REST+Persistence working you are so happy, but after a while of writing such tests you notice that you spend so much time waiting for deployment between test runs. This is a real pain for test developers.

Well, luckily again we have got Arquillian JRebel extension that uses awesome JRebel to speed up test development by deploying only once and using hot redeploy of changed classes.

Currently there (persistence-impl-1.0.0.Alpha6) is a little problem with Arquillian JRebel and Persistence extensions integration (ARQ-1572). The datasets are not being hot deployed. For the time being you may use simple workaround of adding datasets directory directly to the deployed archive:
ShrinkWrap.create(WebArchive.class, "someTest.war")
            .addAsResource("datasets")
I hope that me and Bartosz Majsak will soon come out with elegant solution to this little inconvenience. Happy and faster test development!