Table Of Contents

Previous topic

jcompile – Java 6 Compiler API Wrapper

This Page

junit – Annotations for JUnit 4.X

JUnit 4 Annotations

The following Python decorators produce Java annotations and correct signatures. For example the Java annotation Before corresponds to the Before decorator in Jython which is defined as

Before = annotation.extract(Before)(signature("public void _()")

Annotations on static method like BeforeClass are defined as

BeforeClass = annotation.extract(BeforeClass)(signature(“public static void _()”)

Following annotations are supported

  • Before
  • After
  • Test
  • BeforeClass
  • AfterClass
  • Ignore

The Ignore decorator is defined as

Ignore = annotation.extract(Ignore)

An appropriate signature has still to be added.


A simple test runner called testClasses was implemented. It uses traceback extraction to analyze Python exceptions and writes failure messages to stdout.

Parameters:testclass – a sequence of Jython classes. All of them have to be decorated by jannotations.JavaClass. At least one method decorated with @test must be implemented. Otherwise test initialization fails and a ValueError exception is raised.


from jynx.lib.junit import*
from java.lang import Object
from jynx.jannotations import JavaClass

class TestClass(Object):

    def once(cls):
        print "\nRun 'TestClass' tests ..."

    def nullTest1(self):
        assertTrue("length of empty list is not 0", len([]) != 0)

Running the TestClass with the testClasses runner


yields following result

Run 'TestClass' tests ...

There was 1 failure:

   1) nullTest1(TestClassBase)

      java.lang.AssertionError: length of empty list is not 0
      assertTrue("length of empty list is not 0", len([]) != 0)
      at C:\lang\Jython\jython2.5rc4\Lib\site-packages\jynx\tests\ : 48