Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: Using Mock Objects on RIM API to running Unit Tests on the Computer ...

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 :Smiley Sad

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Using Mock Objects on RIM API to running Unit Tests on the Computer ...

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.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: Using Mock Objects on RIM API to running Unit Tests on the Computer ...

> 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?

 

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Using Mock Objects on RIM API to running Unit Tests on the Computer ...

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.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 81
Registered: ‎07-16-2008
My Device: Curve - 8320
My Carrier: AirTel

Re: Using Mock Objects on RIM API to running Unit Tests on the Computer ...

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.

*(Vipul)() ;
Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Using Mock Objects on RIM API to running Unit Tests on the Computer ...

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
}

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 81
Registered: ‎07-16-2008
My Device: Curve - 8320
My Carrier: AirTel

Re: Using Mock Objects on RIM API to running Unit Tests on the Computer ...

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 {

}

 

 

  1. The test case itself implements an interface, that might be used in the test ?
  2. How is the actual code uses TransportFactory ?
  3. I failed to spot the use of buildTransport() in the test case.

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  :-)

 

*(Vipul)() ;
Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Using Mock Objects on RIM API to running Unit Tests on the Computer ...

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.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter