Annotation Interface Z2UnitTest


@Retention(RUNTIME) @Target(TYPE) @Inherited public @interface Z2UnitTest

Configuration for a unit test class that is run with the z2unit test runner. Use like this:

 @RunWith(Z2UnitTestRunner.class)
 @Z2UnitTest(componentName="myproject/java")
 public class SomeTests {
        @Test
        public void testThis() throws Exception {
                // some test code
        }
 }
 

You may specify custom JUnit unit test runners. For example, a groovy written test using the Spock test specification framework would look like this:

 @RunWith(Z2UnitTestRunner.class)
 @Z2UnitTest(componentName="spock_tests", runWith=Sputnik.class)
 class HelloSpockZ2 extends Specification {
 
        def "my first test"() {
                setup:
                        def x = new ArrayList();
                when:
                        x.add("Hello")
                then:
                        x.size() == 1
        }
 }
 

Here the module name is spock_tests and specifying this as component name is a short hand for spock_tests/java. Furthermore note that Sputnik is the Spock-supplied JUnit test runner. Please check the wiki for more information on how to run Groovy on Z2.

Sometimes it can be handy to initialize other components before running a test (e.g. a Spring application context). Instead of using a super class to do static initialization, you can instead use the dependencies() attribute to specify Z2 components to be prepared before the test run. For example a test that requires a Spring application context to be initialized and makes use of Spring configuration could look like this:

 @RunWith(Z2UnitTestRunner.class)
 @Z2UnitTest(
   componentName="com.acme.modulex/java",
   dependencies={"com.acme.modulex/applicationContext"}
 )
 @Configurable
 public class BusinessTests {
 
   @Autowired
   private SomeService someService;
   
   @Test @Transactional
   public void testSomething() {
     // ...
   }
 
 }
 
See Also:
Z2UnitTestRunner
  • Element Details

    • runWith

      Class<? extends org.junit.runner.Runner> runWith
      A Unit Test Runner to be used when executing the unit test in the target environment. That is, assuming that the unit test would be annotated as
      @RunWith(MyRunner.class)
      when run outside of z2 Unit, then, in order to run the unit test with the z2 Environment, but using the runner MyRunner, the specification of the runner is added to this annotation as
      @Z2UnitTest(runWidth=MyRunner.class)
      In other words: By adding the runner to a declaration of this annotation, you can force a non-default test runner. This is particularly useful when running tests using JUnit extensions like the Groovy-based Spock toolkit. In that case for example, you would use the Sputnik JUnit Runner of the Spock framework.
      Default:
      org.junit.runner.Runner.class
    • componentName

      String componentName
      Name of the java component that contains the test class. A short form may be used for java components ending with "/java". Alternatively the component name may be specified using the system property z2unit.componentName
      Returns:
      Default:
      ""
    • className

      String className
      Name of the test class. Defaults to the current class name. It is however possible to specify a different test class name to be used server-side. As a result, z2Unit will report test execution events on that class which may not match the structure of the client side class. Alternatively the class name may be specified using the system property z2unit.className
      Returns:
      Default:
      ""
    • z2unitUrl

      String z2unitUrl
      URL to send the test request to. Defaults to http://localhost:8080/z2unit/run. The URL may alternatively be defined by the system property z2unit.z2unitUrl.
      Returns:
      Default:
      ""
    • z2unitUser

      String z2unitUser
      Username to send for authentication. Defaults to "z*". The username may alternatively be defined by the system property z2unit.z2unitUser.
      Returns:
      Default:
      "z*"
    • z2unitPassword

      String z2unitPassword
      Password name to send for authentication. Defaults to "z". The password may alternatively be defined by the system property z2unit.z2unitPassword.
      Default:
      "z"
    • configFile

      String configFile
      A z2unit configuration file. Defaults to z2unit.properties. When found on the classpath, the properties specified in that file will be loaded into the client-side system properties.
      Returns:
      Default:
      ""
    • dependencies

      String[] dependencies
      Runtime dependencies specified as component names. Before executing a test on the z2 side, these components will be looked up as IDependencyComponent and, if successful, be asked to IDependencyComponent.prepare().
      Default:
      {}