Translating JUnit Tests

  • J2ObjC enables the translation of Java unit tests, written using JUnit, into Objective-C code for execution on OS X.

  • This translation process ensures that the original Java code's behavior remains consistent after conversion and validates its functionality within the Objective-C environment.

  • Developers can utilize J2ObjC's tools and libraries to compile, link, and execute these translated tests, similar to running JUnit tests in Java.

  • Comprehensive build processes for managing and running numerous unit tests can be established using examples like the 'tests.mk' file in the J2ObjC project source.

Well-engineered software projects usually have lots of unit tests to verify them. For Java projects, JUnit is the most common unit test framework. J2ObjC provides support for translating unit tests, so they can be executed as binaries on OS X. This verifies that the translation didn't change the semantics (behavior) of the translated classes, and verifies that the translated code runs as Objective-C code.

Translating Tests

Run j2objc with a junit.jar in the classpath. A copy of this jar file is included in the j2objc distribution, named lib/j2objc_junit.jar:

# Example: J2ObjC bundle unzipped into a ~/tools directory
export J2OBJC_HOME=~/tools/j2objc
${J2OBJC_HOME}/j2objc -classpath ${J2OBJC_HOME}/lib/j2objc_junit.jar MyUnitTest.java

Linking Tests

Link with the libjunit.a library in the J2ObjC distribution's lib/ directory, using the compiler's -l flag:

${J2OBJC_HOME}/j2objcc -ObjC -o mytest -ljunit MyUnitTest.m

Running Tests

Run the test executable with the names of one or more tests and/or test suites, like JUnit tests are run in Java. The names can either be the fully-qualified Java name (with package), or the equivalent translated name. For example, the com.company.MyUnitTest test class can also be specified as ComCompanyMyUnitTest.

./mytest org.junit.runner.JUnitCore com.company.MyUnitTest  # or com.company.Test2

org.junit.runner.JUnitCore is one of JUnit's test runners, which can run either JUnit3 or JUnit4 tests. Any other JUnit runner can be used, though.

Building Tests

A good example of how to use make to build and run a large set of unit tests is in j2objc/jre_emul/tests.mk, in the project source code.