Translating JUnit Tests
Stay organized with collections
Save and categorize content based on your preferences.
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.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eJ2ObjC enables the translation of Java unit tests, written using JUnit, into Objective-C code for execution on OS X.\u003c/p\u003e\n"],["\u003cp\u003eThis translation process ensures that the original Java code's behavior remains consistent after conversion and validates its functionality within the Objective-C environment.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can utilize J2ObjC's tools and libraries to compile, link, and execute these translated tests, similar to running JUnit tests in Java.\u003c/p\u003e\n"],["\u003cp\u003eComprehensive build processes for managing and running numerous unit tests can be established using examples like the 'tests.mk' file in the J2ObjC project source.\u003c/p\u003e\n"]]],[],null,["# Translating JUnit Tests\n\nWell-engineered software projects usually have lots of unit tests to verify them. For Java projects,\n[JUnit](http://junit.sourceforge.net/) is the most common unit test framework. J2ObjC provides\nsupport for translating unit tests, so they can be executed as binaries on OS X. This verifies that\nthe translation didn't change the semantics (behavior) of the translated classes, and verifies that\nthe translated code runs as Objective-C code.\n\nTranslating Tests\n-----------------\n\nRun j2objc with a `junit.jar` in the classpath. A copy of this jar file is included in the j2objc\ndistribution, named `lib/j2objc_junit.jar`: \n\n # Example: J2ObjC bundle unzipped into a ~/tools directory\n export J2OBJC_HOME=~/tools/j2objc\n ${J2OBJC_HOME}/j2objc -classpath ${J2OBJC_HOME}/lib/j2objc_junit.jar MyUnitTest.java\n\nLinking Tests\n-------------\n\nLink with the `libjunit.a` library in the J2ObjC distribution's `lib/` directory, using the\ncompiler's `-l` flag: \n\n ${J2OBJC_HOME}/j2objcc -ObjC -o mytest -ljunit MyUnitTest.m\n\nRunning Tests\n-------------\n\nRun the test executable with the names of one or more tests and/or test suites, like JUnit tests are\nrun in Java. The names can either be the fully-qualified Java name (with package), or the equivalent\ntranslated name. For example, the `com.company.MyUnitTest` test class can also be specified as\n`ComCompanyMyUnitTest`. \n\n ./mytest org.junit.runner.JUnitCore com.company.MyUnitTest # or com.company.Test2\n\n`org.junit.runner.JUnitCore` is one of JUnit's test runners, which can run either JUnit3 or JUnit4\ntests. Any other JUnit runner can be used, though.\n\nBuilding Tests\n--------------\n\nA good example of how to use `make` to build and run a large set of unit tests is in\n[j2objc/jre_emul/tests.mk](https://raw.github.com/google/j2objc/master/jre_emul/tests.mk), in the\nproject source code."]]