Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit UnsatisfiedLinkError: android.util.Log.isLoggable(Ljava/lang/String;

I'm getting the following error when I run a JUnit test on an android app that uses apachehttp-client. The app runs successfully on my test device and on an emulator. A login JUnit test also passes but the rest of the JUnit tests fail whenever the app tries to use the apachehttp-client to read data from a server.

The test seems to fail at httpClient.execute

try {
        URL businessPartnersResource = new URL(
                session.getServer().getUrl(), "BusinessPartners");

        HttpGet request = new HttpGet(businessPartnersResource.toURI());
        session.attachToRequest(request);

        HttpClient httpClient = HttpClientFactory.getClient();
        HttpResponse response = httpClient.execute(request);

        int status = response.getStatusLine().getStatusCode();

        switch (status) {
        case HttpsURLConnection.HTTP_OK: {

Here's the Failure Trace

java.lang.UnsatisfiedLinkError: android.util.Log.isLoggable(Ljava/lang/String;I)Z
at android.util.Log.isLoggable(Native Method)
at org.apache.http.client.protocol.RequestClientConnControl.process(RequestClientConnControl.java:76)
at org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:251)
at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:168)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:458)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
like image 346
Pip Avatar asked Mar 12 '15 15:03

Pip


3 Answers

Frustrating, but you'll have to use a RobolectricTestRunner instead of the standard JUnit one.

isLoggable is a native method so its not available at runtime.

If you use Volley or the apachehttp-client, the biggest issue is that isLoggable is called as a field instantiation, for example:

private static final DEBUG = Log.isLoggable(..);

Which completely messes things up given that this method is simply not available at all within those libraries at runtime, so even an AndroidTestCase can't save you.

Luckily, Robolectric handles it perfectly.

Edit April 4, 2017

JUnit 5 is available now and I have not tested with this testrunner.

like image 85
VicVu Avatar answered Nov 01 '22 20:11

VicVu


I had the same issue, try extending AndroidTestCase in your test class. The error is due to classes that can be found only on the android runtime (Device\Emulator)

like image 33
Elhanan Mishraky Avatar answered Nov 01 '22 20:11

Elhanan Mishraky


For me, it happened when running a specific test on kotest. I had to run all the tests at once instead of one by one.

like image 1
Rafael Ruiz Muñoz Avatar answered Nov 01 '22 21:11

Rafael Ruiz Muñoz