Show TOC

Adding JUnit Tests to a Development ComponentLocate this document in the navigation structure

Use

You can enable and add unit tests into a development component (DC) build. This is done using the Build Infrastructure Extension (BIE). tc/bi/testtech2 integrates the execution of standalone unit tests into the DC build. You can add unit tests to a Java DC and the Build Plugin Framework runs the tests during the DC build. If a test fails, the DC build also fails. If all tests succeed, you can find the test results and (optionally) code coverage results in the DC´s log folder. Note that you can use only JUnit tests from the different unit test types.

The following sections explain the concepts and provides details on the tasks for a test developer:

  • Adding unit test support to your DC (default <junit> Ant task)

  • Setting the DC build options

  • Defining test dependencies

The JUnit tests are placed in a separate folder in the DC: test/java . If special libraries are required for test compilation or execution, you can define a test build-time dependency to a compilation public part of another DC. If you need a library only for test execution, define a test dependency to an assembly public part. These test dependencies are treated separately from the productive DC dependencies (so the productive DC model is not polluted with test dependencies). The compiled test classes are also separated from the productive classes.

Figure 1: Integration of unit test execution into the DC build

When the DC build is started, the JUnit tests are compiled and then executed. JUnit 3.x and 4.x tests are supported. If code coverage is switched on, the classes under test are instrumented and code coverage calculations are performed. All classes from the normal package folders are instrumented and processed. In other words, the productive Java classes account for 100% code coverage. The results are written to the DC´s log folder:

Procedure

Adding unit test support to a DC (default <junit> Ant task)

To enable JUnit test execution for your DC, you have to adapt the DC metadata. To do that, open the DC context menu and choose Enable Unit Test Support .

The following changes are applied over your DC:

  • The folder test/java is created and marked as a package folder.

  • The folder test/TEST-INF is created and marked as a source folder.

  • A build-time dependency to the def infrastructure public part of the DC tc/bi/testtech2 in SAP_BUILDT is created.

  • Two build-time dependencies to the DC public.test.facade are created:

    • To the compilation public part api , which contains the JUnit library (so that your tests compile).

    • To the infrastructure public part apiinfra , which also contains the JUnit library (must be available at plug-in runtime).

Note

If you want to measure your test code coverage, you must provide the Emma library in a separate DC. Note that SAP cannot provide this third party tool due to legal reasons.

Create a DC of type Build Infrastructure Extension with an infrastructure public part. This public part must contain the emma.jar and emma-ant.jar libraries.

For more information about Emma , see http://emma.sourceforge.net .

For more information about creating DCs, see Creating Development Components . For more information about public parts, see Managing Public Parts .

Now you can add your JUnit source files to the test/java folder. During the next DC build, the tests are automatically executed. You can customize the behavior of the test execution using build options and test descriptors. The test/java folder is marked as a package folder so you can also run the tests directly from the Studio with Start of the navigation path Run As Next navigation step JUnit Test End of the navigation path.

Setting the build options

The tc/bi/testtech2 BIE can be configured using build options. Test execution is switched on by default, but code coverage measurement is switched off (because you must provide the Emma library first or you will get a build error).

Build Option

Description

Possible Values

com.sap.junit.executetests

Tests are executed only if this option is set to true .

true (default), false

com.sap.junit.codecoverage

Code coverage calculation are performed if set to true .

true, false (default)

com.sap.junit.printsummary

Prints test and code coverage summary to the DC log folder.

true (default), false

com.sap.junit.haltonfailure

DC build is broken if a test fails (but all tests will be executed).

true (default), false

com.sap.junit.formatter

Type of reporting output (see Ant task documentation).

xml, plain (default), brief

com.sap.junit.showoutput

Show output of unit tests in build.log file.

true, false (default)

com.sap.junit.maxmemory

Max memory for unit test VM.

xxxxm (default = 768m)

com.sap.junit.includeFiles

Comma-separated list of Ant include patterns.

Ant patterns (default = "**/*.java")

com.sap.junit.excludeFiles

Comma-separated list of Ant exclude patterns.

Ant patterns (default = empty)

The build options are set in the Change Management Service (CMS) of NWDI in the Build Variants dialog. If you want to overwrite build options for a single DC, the easiest way is to create a properties file named build.properties in the cfg folder of the DC. For more information about managing build options in CMS, see Managing Build Options .

Defining "test" dependencies

You can define special test dependencies to compilation public parts if you need additional libraries to compile your tests. The tc/bi/testtech2 BIE will add these libraries to the test compilation classpath and to the test execution classpath. In a similar way, you can define dependencies to assembly public parts if you need certain libraries for test execution (for example, a logging lib). These libraries are added to the test execution classpath. The test dependencies are not visible for other Ant tasks that are executed during the DC build. The compilation classpath for the productive Java classes is not changed.

The Developer Studio supports the creation of test dependencies. If you create a Build Time dependency to a compilation or assembly public part, you can select the test qualifier. To do that:

  1. In the Development Infrastructure perspective, select the DC in which you want to change the dependencies.

  2. Open the Component Properties view, and choose the Dependencies tab page.

  3. Select the required DC from the list of dependencies. Select the type of dependency for the required DC.

  4. From the right-hand side dependency details, select test from the dropdown menu.