02-17-2010 07:55 AM
I know this problem because I am not a fan of simulator based unit testing but did not find a good way yet to mock the net_rim_api.jar
For the ME classes there are some mocks available:
http://mockme.sourceforge.net/
RIM did not even have to provide a framework but a mockable version of net_rim_api.jar :![]()
02-17-2010 09:58 AM
rim announced a unit test on the last dev conference.
i test my network code using plain junit by using interfaces. just do an empty impl of an interface and use it in the test, the app uses a rim-api impl.
webservice calls, for example, are written without any rim-dependent code so they execute just fine in desktop java as well. ksoap2 provides a HttpTransportSE class instead of HttpTransport for this, all you have to do is to put a getTransport into an exchangeable interface.
02-18-2010 07:01 AM
> rim announced a unit test on the last dev conference.
they did? I only remembered the RIMUnit presentation which was 3rd party and nothing new about it.
> i test my network code using plain junit by using interfaces.
> just do an empty impl of an interface and use it in the test, the app uses a rim-api impl.
But you have problem when having imports to net.rim classes in the java file. The unit test won't start because net_rim_apir.jar contains native and incomplete classes which causes reflection errors.
Never had those problems?
02-18-2010 07:41 AM
ah, ok, thought rimunit would be done by rim, didn't attend the sessions myself.
sure, i avoid these problems by not using UI or net.rim classes in the unit tests, masking them with interfaces if needed.
02-18-2010 10:44 PM
There would be many classes and public methods within those classes that we write using RIM specific APIs. To test such public methods, we write unit tests.
Could you please paste code of one such small method that uses RIM API (actual code) and the unit test method for that. It would be very helpful for people (like me) looking for effective ways of unit testing.
02-23-2010 10:10 AM
ok, here is some part of my test class for webservice unittests with ksoap2.
you have to include the ksoap2 j2se full jar.
This code is executed with the JUnitTest framework in eclipse, not on the blackberry or simulator.
TransportFactory is an interface to create the transport (either HttpTransport for device/simulator or HttpTransportSE in the tests), only method is buildTransport.
WebserviceResultException is an own impl to give additional info.
public class WebserviceTest extends TestCase implements TransportFactory {
//webservice fields
protected void setUp() throws Exception {
super.setUp();
//init webservices
}public Transport buildTransport(String serviceName) throws IOException {
String url = "http://servername:port"
return new HttpTransportSE(url);
}private String login() throws IOException, XmlPullParserException, WebserviceResultException {
return webservice.login("username", "password"); //returns sessionID
}//test methods now
public void testLogin() throws Exception {
assertNotNull(login());
}public void testSetSessionTimeout() throws Exception {
assertTrue(webservice.setSessionTimerValue(login(), 3600));
}
//and some dozen more
}
02-25-2010 04:35 AM
First of all, Thanks Simon :-)
Next, I have a couple of queries in my mind after looking into the code snipplet.
public class WebserviceTest extends TestCase implements TransportFactory {
}
Can you please let the interested audience know (including myself), How the OUT (Object Under Test) is written, such that at run time it uses methods in net_rim_api.jar and while unit testing with JUnit, the same code is ran using J2SE classes.
I am very much interested in knowing the mechanism.
Thanks in Advance :-)
02-25-2010 04:48 AM
buildTransport is used to get the ksoap2 transport in the webservice implementation.
on the blackberry another class implements the interface and returns a HttpTransport instance, on the unittest the implementation returns a HttpTransportSE.
whenever you have to use j2me specific code in your test code you have to hide it using interfaces.
my webservice implementations do not use any j2me specifics, except the transport.
I won't post code of my webservice here.
using JUnit is only possible if you write your code in a way that does not use rim api code in networking classes or makes it exchangable using interfaces.