21

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?

Ben S
  • 68,394
  • 30
  • 171
  • 212
  • 2
    +1 for openly admitting you're asking for help with homework. – Soviut Jun 20 '09 at 01:22
  • The assignment doesn't actually have any testing requirements. We were given free reign over methodology, so I thought I'd try out TDD... It's just not working as easily as I expected. – Ben S Jun 20 '09 at 01:24
  • FWIW, at http://stackoverflow.com/questions/856115/should-one-test-internal-implementation-or-only-test-public-behaviour/859688#859688 I propose a theory as to when it's necessary to have unit-testing against mocked interface ... and, in this case / your present circumstance, maybe it's *not* necessary, and not the most efficient way to proceed. – ChrisW Jun 20 '09 at 01:37
  • That's not to say that TDD (and, in particular, staged deployment with unit testing before integration testing before system testing) is bad or anything, just that it's not necessarily the best/quickest way given this particular project+team+tools. – ChrisW Jun 20 '09 at 01:40
  • This question is more about the *how* then it is about the when or why. I understand the pros and cons of TDD. – Ben S Jun 20 '09 at 02:00

2 Answers2

19

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

Community
  • 1
  • 1
Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
1

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
ChrisW
  • 54,973
  • 13
  • 116
  • 224
  • 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. – Ben S Jun 20 '09 at 01:03
  • To my knowledge I would have to shut down my application, simulate a USB connection to it and re-upload the binary. This could be scripted, but is still a much bigger pain than clicking the JUnit Re-run button in eclipse. – Ben S Jun 20 '09 at 01:17
  • Also, replying to comments in another comment allows for notifications and provides a better history than copy-pasted edits. – Ben S Jun 20 '09 at 01:18
  • Yes, sorry, however comments are unformatted and the comment-length is limited. – ChrisW Jun 20 '09 at 01:25
  • Maybe look into scripting then. For Windows Mobile, it's a single keystroke (F5) within Visual Studio to build the code, start the emulator (if the target is the emulator rather than the real device, and if the emulator isn't already started), copy the built executable to the target (emulator or hardware), and start it running with the debugger attached. It's kind of slow, but way less than a minute (unless I'm testing something that needs the device to reboot). – ChrisW Jun 20 '09 at 15:33