There are three bundles involved:
A typical test looks like
class MyTest extends junit.framework.TestCase { public void runTest() { ... do some work assertTrue(someflag) } }As long as these test do not depend on a running framework, they are easy to run using normal tools, but when they depend on the framework, the standard test running tools become insufficient.
Thus, the junit bundle allows remote access to test cases running on a framework, from any normal test tool running on a developer machine.
Example: register a test suite into the framework.
TestSuite suite = new TestSuite("example1"); suite.addTest(new MyTest()); Hashtable props = new Hashtable(); props.put("service.pid", suite.getName()); bc.registerService(Test.class.getName(), suite, props);
http://where: /junit?id=
The only interface that registered tests need to implement is junit.framework.Test
The junit bundle thus accesses the tests via BundleContext.getService() and needs ServicePermission, if FW security is active.
Note how *only* step 2) is extra work compared to writing standard JUnit tests.
The client class name is:
org.knopflerfish.service.junit.client.JUnitClientThe target host and test id is passed to the client as a system property "suite.url"
This client test class extends TestSuite and act as a proxy to the actual test, and can thus be passed to any test runner, as
junit.swingui.TestRunner or junit.textui.TestRunner
as well as Ant's "junit" task.
Example: Using the Swing TestRunner
> java "-Dsuite.url=http://localhost:8080/junit?id=example1" \ junit.swingui.TestRunner \ org.knopflerfish.service.junit.client.JUnitClientThis will run the test with id "example1" on localhost:8080
Note that the class path must be set fo find both junit.jar and the junit_all-1.0.0.jar bundle.
> enter junit junit> help Available junit commands: list [-help] - List available tests run [-help] [-out #file#]- Run a test and dump XML results to a file or console.
The servlet is also capable of exporting the test results as plain HTML. In this case, the client proxy isn't needed. Just point your browser at
http://and you'll get a list of available tests. From this list you can select suites and individual tests to run. The result will be presented as HTML. Note2:: /junit
The junit-bundle will look up all services registered under the class junit.framework.TestListener in the running framework and add them to the TestResult as listeners.
Example: register a test listener into the framework.
TestListener listener = new MyTestListener(); bc.registerService(TestListener.class.getName(), listener, null);
Example: run the swing Test runner from Ant
> ant -Dtest.id=example1 junit_extExample: run Ant's junit task
> ant -Dtest.id=example1 junit_antTip: Bundles can be installed using the telnet console. The telnet console is installed by the default init.xargs. In this case a bundle can be installed and started by
> ant install startThe following Ant properties are set as default in bundlebuild.xml:
http.host localhost http.port 8080 junit.runner.class junit.swingui.TestRunner junit.formatter plain junit.outfile junit