1

I want to write functional tests for my Codename One app.

To do this, I need to be able to mock some server responses, so that they return exactly what I need for the specific test.

One thing that I tried to make the functions that send the HTTP requests take ConnectionRequest as a parameter, this way I can inject something to simulate network latency and then call the callback with the mocked response.

However, when I try to inject the mocks of ConnectionRequest in the prepare method of a unit test, they are still injected after the app is started, which does not work for my case, since I sent HTTP request on the start of the app.

Is it possible to mock ConnectionRequest or do something equivalent before starting the app?

1
  • We don't have anything like this in Codename One. You might be able to hack some JavaSE mock frameworks to work with the simulator though.
    – Shai Almog
    Oct 16, 2016 at 2:22

2 Answers 2

1

You can slow down connectivity in the simulator which will mock slow connection on devices or turn it off completely to simulate No internet connection. Find the options in your simulator under Simulate -> Network. Change this setting to Slow connection or Disconnected and re-run your app.

Point to a wrong URL to simulate 404 error.

Return unreadable data from your server side code to simulate IOException or NullPointer exception.

While performing this, make sure your Network Monitor is visible. Most of other responses you want could be done on your server side.

1
  • Thanks for the answer! But the thing I want in this case is for example, a server endpoint may return two possible responses, and I want to test that the app acts appropriately for each one. Since the functional tests are automated, I can't hardcode the response in the server. I am looking for something like docs.angularjs.org/api/ngMock/service/$httpBackend in Angular, basically you tell: "For this automated test, expect this HTTP request, and respond with this HTTP response". After some research, I think that this is not possible in Codename One. Oct 15, 2016 at 17:18
0

Here is what I ended up doing. I am posting this if someone needs a workaround for the automated tests.

  1. Write an interface called HTTPRequestInterface, which exposes the neccessary methods to construct an HTTP query
  2. Create an implementation of HTTPRequestInterface called say HTTPRequest, which is just a wrapper around ConnectionRequest
  3. Create a class for creating HTTPRequests, say HTTPRequestFactory, which has a static method called create and a method called setMock. This class creates a normal HTTPRequest in case no mock was set and the specified mock in case a mock was set.
  4. Replace all ConnectionRequest with HTTPRequestFactory.create()
  5. Create a class InitialSetup, which has two methods: setInitialization, which accepts a Runnable to be executed, and init method, which just runs the Runnable.
  6. In the start method, call InitialSetup.init
  7. Now in the tests, in the prepare method, I can set the desired mock, and the app when not running automated tests will send queries to the actual server.

Requires some effort, but the automated tests with mocked backend bring confidence that I think is definitely worth it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.