Development:Writing JUnit tests

From Tom

Jump to: navigation, search
Dev : Developping

Writing JUnit tests  > Writing an Ant task  > Creating a new plugin  > Messages and exceptions : Using the logger  > PIL language  > Description of ADT  > Description of external libraries  > New parser's architecture

Contents

JUnit tests

JUnit is a framework which allows to test features and methods of an application. It also allows to check that the software behaviour is correct. Therefore any new feature in Tom or Gom should be checked by a new JUnit test. Although there is a backward compatibility between JUnit 3.x and 4.x, since we use JUnit 4.5, it is important to choose to write JUnit 4.x tests rather than JUnit 3.x tests.

Writing a JUnit test

Import

The JUnit API changed a lot in the 4th version : junit.framework.*, junit.runner.* and junit.textui.* should not be used anymore. They have been replaced by org.junit.* and subpackages (org.junit.runner, org.junit.runners, etc.). As a test class does not extend of TestCase anymore, the import of org.junit.Assert is mandatory when using assert methods (except if you simply use the builtin assert). When using a lot of different assert methods in the test, the best is to import them like this :

import static org.junit.Assert.*;

Annotations

JUnit4 uses annotations to define test methods, setup methods, etc. Here is a list of useful annotations :


@Test

This annotation defines a method as a test method (the method name does not have to begin with "test" anymore).

@Test
public void myMethod1() {
  ...
}
 
@Test
public void myMethod2() {
  ...
}


@Before

This annotation defines a method as a method which is called before each test. It is used to replace the old "setUp()" method.

@Before
public void initVariables() {
  ...
}
@Test
public void myMethod1() {
  ...
}


@After

This annotation defines a method as a method which is called after each test. It is used to replace the old "tearDown()" method.

@Test
public void checkTemporaryFiles() {
  ...
}
@After
public void emptyTemporaryDirectory() {
  ...
}


@BeforeClass

This annotation defines a method as a method which is called before any of the test methods in the class (for instance if you need to initialize a shared resource).

@BeforeClass
public static void initConnectionToDB() {
  ...
}


@AfterClass

This annotation defines a method as a method which is called after any of the test methods in the class (for instance if you need to release a shared resource).

@AfterClass
public static void closeConnectionToDB() {
  ...
}


@Ignore

This annotations allows to temporarily disable a test method or a class. Ignore a test method :

@Ignore
@Test
public void myMethod() {
  ...
}

Ignore a test class :

@Ignore public class TestNewFeature {
   @Test
   public void method1() {
     ...
   }
   @Test
   public void method2() {
     ...
   }
}


@RunWith

This annotation allows JUnit to invoke the class it references to run the tests in that class instead of the runner built into JUnit. It used with the following annotations.


@Parameters

This annotation allows to provide parameters which will be used by the test class (with @RunWith(Parameterized.class)).

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
 
@RunWith(Parameterized.class)
public class MyTestClass {
  private Type1 attr1;
  private Type2 attr2;
  public MyTestClass(Type1 param1, Type2 param2) {
    attr1 = param1;
    attr2 = param2;
  }  
 
  public static main(String[] args) {
    org.junit.runner.JUnitCore.main(MyTestClass.class.getName());
  }
 
  @Parameter
  public static Collection<Object[]> createTestParams() {
    ...
    ...
    return params;
  }
 
  @Test
  public void method1() {
    ...
  }
}


@SuiteClasses

This annotation specifies the classes to be run when a class annotated with @RunWith(Suite.class) is run.

package test;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
 
@RunWith(Suite.class)
@SuiteClasses({
  AnOtherTestClass.class,
  MyTestClass.MyInnerClass.class,
  ...})
public class MyTestClass {
  public static main(String[] args) {
    org.junit.runner.JUnitCore.main(MyTestClass.class.getName());
  }
  ...
  public static class MyInnerClass {
    @Test
    public void myInnerClassTestMethod() {
      ...
    }
  }
}


Resources

Developping

Writing JUnit tests  > Writing an Ant task  > Creating a new plugin  > Messages and exceptions : Using the logger  > PIL language  > Description of ADT  > Description of external libraries  > New parser's architecture

Development corner
Developping Tom Compiler :: Creating a Tom release
Personal tools
Create a book