Overview

maven-qunit-plugin

This is a maven plugin for adding a qunit runner that will pick up your javascript tests and execute them.

Add the following to the <plugins> section of of your pom.xml

<plugin>
   <groupId>com.atlassian.maven.plugins</groupId>
   <artifactId>qunit-maven-plugin</artifactId>
   <version>1.0</version>
   <executions>
      <execution>
         <phase>test</phase>
         <goals>
            <goal>test</goal>
         </goals>
      </execution>
   </executions>
   <configuration>
      <includes>
         <include>*/**/*test.js</include>
      </includes>
   </configuration>
</plugin>

The qunit maven plugin supports most of the options that the surefire plugin supports such as <includes> and <exlcudes> and more.

You can also run the server to execute the tests manually by calling

mvn qunit:run-server

Requiring javascript libs for your test

The idea of the plugin is that you don't need to write the html boiler plate if you are simply unit testing javascript. But you need a way to include your javascript files to test.

Simply add the following to the top of your test and it will be pulled into the test html page generated.

 Qunit.require('path/to/some/lib.js');
 Qunit.require('https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');

Requiring CSS stylesheets for your test

If you need to include a css stylesheet you can include an external CSS stylesheet or one bundled in your application.

Simply add the following to the top of your test and it will be pulled into the test test html page generated.

 Qunit.requireCss('path/to/some/stylesheet.css');

Fixtures

Sometimes you need to test with a DOM structure. Qunit allows there to be a fixture section in the test page which is reset on every test method run.

If you simply add a {testname}-fixture.html file next to the test it will be picked up and used as the fixture html. eg. the test is called simple-test.js then the fixture should be called simple-test-fixture.html

Resource locations

By default the plugin will use the maven projects test class elements and look for resources there. If you want to override the resource locations or wish to be able to alter tests without having to restart the run-server goal you can specify a resourceLocations section in the configuration.

<configuration>
    <resourceLocations>
        <resourceLocation>${project.basedir}/src/main/resources</resourceLocation>
        <resourceLocation>${project.basedir}/src/test/resources</resourceLocation>
    </resourceLocations>
</configuration>