Monday, 5 September 2011

Integrating SureAssert with Existing Projects

I recently blogged about SureAssert’s ability to do automatic, continuous, declaration driven testing using annotations, which could make JUnit test classes obsolete. When picking up any new and useful development tool, the first question asked is usually “how can it help my existing project?” The answer here is that SureAssert integrates with existing JUnit test code, which gives you the benefit of having your existing JUnit tests continuously monitored and executed.

To integrate SureAssert with an existing project, you’ll need to add the SureAssert Annotation jar file, which is currently: org.sureassert.uc.annotation-1.1.0.jar, to your project. This JAR file isn’t currently available from Maven Central, so you’ll need to add it manually to your local repository using the maven install file command. You’ll need to execute something like:

mvn install:install-file -DgroupId=sureassert -DartifactId=annotations -Dversion=1.1.0 -Dpackaging=jar -Dfile={path to}/org.sureassert.uc.annotation-1.1.0.jar

Having made this JAR file available to your application, the next step is to annotate the classes that you want SureAssert to monitor. Going back to the Calculator example used in my last blog, then for SureAssert to pick-up Calculator's JUnit test class, CalculatorTest, you'll need to add the following annotation to your Calculator class:

@HasJUnit(jUnitClassNames = "com.sureassert.uc.tutorial.mytests.CalculatorTest")
public final class Calculator {

@UseCase(args = { "1", "2" }, expect = "3")
public int add(int x, int y) {

return x + y;

@UseCase(name = "multiplyTest", args = { "4", "2" }, expect = "8")
public int multiply(int x, int y) {

return x * y /* + 1 */;

   * This method is served by a JUnit test
public int subtract(int x, int y) {

return x - y;


Note that’ll you need the fully qualified class name of your JUnit test as an attribute to the HasJUnit annotation.

The SureAssert plugin with then automatically run the CalculatorTest JUnit when necessary and test failures will be shown as errors:

If your tests follow the FIRST acronym, and your tests are fast then attaching @HasJUnit is a good idea. I would suspect that badly written, slow and end to end tests may make eclipse run slowly. This this case, don’t blame SureAssert, take a look at your tests...

1 comment:

Nathan Dolan said...

That's a good approach if you want to explicitly associate a class with the Junit class (or classes) that test it.

Alternatively, you can enable the "Run JUnits Automatically" option in the Sureassert UC Preferences. That monitors and runs all the JUnits in your projects automatically and reports errors/failures as Eclipse problems.

Either way, after the initial run it'll only run those tests affected by any changes you make (either to the test code or the production code it's testing), and you'll get the continuous coverage reporting.

If you go for the auto-run approach you'll probably want to tell Sureassert not to run some test classes (e.g. integration tests and slow ones) by annotating them with @NoAutoRun.