Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a problem I and others have encountered in jUnit tests. Namely, testBindable() will run in one thread while your service is running in another. The result is that the location service is still setting up and hasn't even thought about using your callbacks when the test hits the end and jUnit clears everything from memory. I am still looking for a good recipe to follow for structuring such a test, but I can suggest a few things you could try. The simplest is just to put a sleep() in your test to give things time to process in the other threads. For example:</p> <pre><code>public void testBindable() throws Exception { Intent startIntent = new Intent(); startIntent.setClass(getContext(), GpsService.class); IBinder service = bindService(startIntent); assertNotNull("Bound to service ",service); getService().setUpProvider(MOCK_GPS_PROVIDER); Location demo = new Location(MOCK_GPS_PROVIDER); demo.setLatitude(22.579937); demo.setLongitude(88.486805); getService().setTargetLocation(demo); TimeUnit.SECONDS.sleep(5); System.out.println("Test Bindable"); } </code></pre> <p>This approach is not ideal for a number of reasons. 5 seconds (or whatever you put in) may not be enough sometimes or too much time in other cases. </p> <p>Alternatively, you can use latches or other mechanisms to detect when your callbacks have executed. However, that approach complicates your code with latches (or you can use mutex locks) that are only used by the test and ideally should not be part of the actual application under test.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload