Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I easily unit test Blackberry code?

Tags:

For my university class we are developing a multi-threaded Blackberry application which allows us to scan for other devices running our application with Bluetooth and then transfer files to each-other by TCP over the Wifi interface, implementing NAT traversal, all the while logging our GPS location. (It's a RIM sponsored Computer Networks class in case that wasn't obvious yet.)

I have grown fond of Test Driven Development and was going to employ it for developing my homework assignment. However, any Blackberry class which I extend or otherwise call during testing gives me a ClassFormatError due to illegal modifiers. I presume this error is caused because the jar with the Blackberry code must have been specially compiled for their proprietary JVM.

So far I've resorted to using the Proxy Pattern and implementing Mock Objects of the proxies. However, this is getting very tedious since I'm inheriting from many native Blackberry classes.

I would also like to avoid having to launch the Blackberry simulator if possible. It can take minutes just to boot it up and this is impractical and annoying for unit tests.

Is there am easy way to unit test my Blackberry code?

like image 213
Ben S Avatar asked Jun 20 '09 00:06

Ben S


2 Answers

Mockup testing

Youre on the right way about mockuping, but I wouldn't advice you to test Blackberry functionality on J2SE platform. I think proxys and mockups should be used in case when there are no testing data available in native source, examples:

"scan for other devices" - there are no other devices but you wan to test scan functionality
"TCP over the Wifi interface" - you want to test it on Storm (no WiFi)
"logging our GPS location" - device location is static, but you want to test other locations

Then you can mockup such functionality using Blackberry platform:
BlackBerry GPS location mockup

Still you can reproduce BlackBerry API class on J2SE from the scratch simply using same names and signatures. That would presume youll have to implement all class functionality by yourself.

Testing j2me without simulator

That would be a really great option, but so far I can't see how to do this.

Testing involves application running and this involves platform simulation. There can be some possibility to test j2me code without whole UI simulator running but I don't know it.

What you can do is test some business logic on Java Standard Edition with minimum code changes.

You still need to run platform dependant functionality testing on simulator, but you can do it in one application, which would be a set of unit tests, like ChrisW already said. Simply run test methods one by one and output results on screen:
Method1 - Passed - 0.03 s
Method2 - Passed - 1.30 s
Method3 - Passed - 0.25 s

J2MEUnit

http://j2meunit.sourceforge.net/:

J2MEUnit is a Java 2 Micro Edition (J2ME) library containing a unit testing framework for J2ME applications. It is based on the source code of the original JUnit, the successful unit testing framework for the standard (desktop) edition of Java, J2SE.

Unit Testing J2ME applications with J2MEUnit and Eclipse
Quick Tutorial to setup & learn J2MEUnit

JMUnit

http://jmunit.sourceforge.net/:

JMUnit is a unit test framework for Java ME (J2ME) based on JUnit. It has the following features:
- Works in both the Sun emulator and on actual devices.
- Is small (tests can be run even on old MIDP 1.0 devices).
- Has a comprehensive collection of Assert methods for checking test failures.
- Both TestCases and TestSuites are supported.
- Includes Ant tasks for running JMUnit tests in a continuous build.
- Has performance monitoring classes inspired by JUnitPerf.

Writing and running JMUnit tests

BUnit

Unit Testing library for RIM Blackberry based on jmunit

http://sourceforge.net/projects/b-unittesting/
BlackBerry Support Community Forums: How to do unit testing my Blackberry Application

Additional

How To - Automate testing with the BlackBerry Simulator

like image 51
Maksym Gontar Avatar answered Sep 16 '22 23:09

Maksym Gontar


when I wanted to to unit-test some Windows Mobile code, I ran them on the simulator/emulator, and/or on the device itself.

This is not practical since I'm not going to launch a simulator which takes nearly a minute to boot after implementing every test/function.

I could boot it, load the software onto it, and run the test ... leave it running ... reload new application software onto it without rebooting, and rerun it. Maybe a Blackberry doesn't allow that?

Also, I could run a whole suite of tests in one shot (no need to reboot between each test/function). Maybe that's incompatible with TDD though, if your habit is:

  1. Write a test case
  2. Run it to ensure it fails
  3. Write the implementation
  4. Run it again, to ensure it succeeds this time
  5. (repeat as above for the next function to be implemented)

It can happen though. Device drivers for example: tedious to debug, because the system may need to boot each time, because they hang the system if they're buggy, because the debugger isn't user-friendly ... environments like that are less interactve, so there's an greater emphasis on:

  • Getting it right first time (so you don't have to debug)
  • Implementing (and writing tests for, and subsequently testing) bigger (perhaps whole) chunks of functionality at once
like image 31
ChrisW Avatar answered Sep 17 '22 23:09

ChrisW