Showing posts with label Arquillian. Show all posts
Showing posts with label Arquillian. Show all posts

Monday, April 28, 2014

Arquillian Graphene 2: JavaScript Unit Testing Examples

I have been doing work with Arquillian for a while. If you need to do integration and unit testing on your JSF application. This is definitely the path to take. It makes testing so much easier to accomplish.

Recently, I have been trying to use Arquillian Graphene 2 to do JavaScript unit testing. I spent a lot of time trying to get the examples on the Graphene 2 - JavaScript Interface wiki to work. I discovered that they were slightly incorrect and the source of my grief. One of the great things about an Open Source world is that I updated the wiki with the correct information.

I have created a couple of Proof of Concept (POC) projects to demonstrate how to use Graphene to do JS testing. The first example uses Graphene in stand-alone mode. This mode allows you to test your JavaScript outside of a container, but using a browser implementation like: PhantomJS, Chrome, Firefox, or Safari.

The Apache Maven NetBeans 8.0 project can be downloaded from Bitbucket here: graphene-js-poc

You will need to execute this from the command line, or use the JS Unit Test custom goal in NetBeans.


The next project is simply a combination of the code from the Arquillian Graphene 2 wiki combined into a more complex project. This project is designed to run in a container. In my case, Glassfish is the container of choice. The slickness of this approach becomes quite apparent when you see the application server start-up, execute the tests, and shutdown gracefully.

The Apache Maven NetBeans 8.0 project can be downloaded from Bitbucket here: graphene-poc.

The project includes a JSF example along with testing JavaScript.

If you need to do unit testing of your JavaScript, this may be an option to consider. Please make sure you read the wiki and understand the limitations of the approach however. You can use my code as an example to help guide you along the path.

Friday, September 06, 2013

Java EE Tip of the Day: @WebFilter Testing Using Arquillian, Warp, and Drone

Introduction

I was looking for a simple solution to test @WebFilter servlet filters. The tests that I had used a lot of mocks, but really just left me unsatisfied. It was not a real world production solution. How would they "really" behave in the actual server environment. I had been using Arquillian to do some testing of our CDI layer, and decided to try it on filters.

I looked at the API, and examples and found really nothing. I found one really "bogus" test example from core api tests that left me really wondering if I should bother. As many of you know though, I love a challenge. I tried a number of ideas including making the filter a JSR-316 @ManagedBean. I am not sure what glue I sniffed for that one (it didn't work...), and using @Inject (that didn't work either... more glue).

What I settled on was using Arquillian Warp and Drone to check the HttpServletRequest and HttpServletResponse after the filter processed it. There are a couple of annotations @BeforeServlet and @AfterServlet that helped me with checking my filters. There are no before and after filter annotations though. That would have made life easier.

Note: The thing to keep in mind with these annotations is that the filtering will have already occurred. You are looking at the post-filter results.

I also wanted to be able to demonstrate another really cool feature called ShrinkWrap, and its Java EE descriptor extensions. This allows you to programmatically create your web.xml file to provide additional flexibility like overriding annotations.

Problem Description

We want to be able to test a @WebFilter that modifies HttpServletRequest attributes, and HttpServletResponse headers. The filter should be tested in isolation, and in a real container to examine the real-world response. The tests should be able to be run on a variety of containers with no modification to the test code.

A Solution

Using MavenArquillianWarpDroneShrinkWrap, and GlassFish 3.1.2.2 embedded we can accomplish all aspects of the testing requirements. I used GlassFish since it is the Reference Implementation for Java EE 5, EE 6 and EE7, and it is a great application server. The container can be swapped out easily in maven with another profile. In the example code below, I used NetBeans 7.4 Beta to do the development.

The code for the project can be found on BitBucket here: maven-arqullian-filter-test

There are two types of examples included. One set of examples demonstrate simple integration tests that use annotations, and annotation override using web.xml. The second set of examples demonstrate dynamic web.xml generation, and overriding. Here are some code snippets from the project.

Popular Posts