Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.d4rk.androidtutorials.java.data.source;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import android.os.Looper;

import com.android.volley.RequestQueue;
import com.d4rk.androidtutorials.java.data.model.PromotedApp;

import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Method;
import java.util.List;

public class DefaultHomeRemoteDataSourceTest {

private DefaultHomeRemoteDataSource dataSource;
private Method parseMethod;

@Before
public void setUp() throws Exception {
if (Looper.getMainLooper() == null) {
try {
Method prepareMainLooper = Looper.class.getDeclaredMethod("prepareMainLooper");
prepareMainLooper.setAccessible(true);
prepareMainLooper.invoke(null);
} catch (Exception ignored) {
Looper.prepare();
}
}
RequestQueue queue = mock(RequestQueue.class);
dataSource = new DefaultHomeRemoteDataSource(queue, "https://example.com");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Move remote data source tests off JVM

The new unit tests build a DefaultHomeRemoteDataSource in a local JVM test. Instantiating that class calls new Handler(Looper.getMainLooper()), but the JVM test environment ships only the stubbed android.os.Looper from android.jar. When ./gradlew test runs with a valid SDK, the call to Looper.getMainLooper() will throw RuntimeException: Method getMainLooper in android.os.Looper not mocked, so the entire test task fails before assertions execute. These tests need to run under Robolectric or the instrumentation androidTest source set, or the DefaultHomeRemoteDataSource should be refactored to avoid framework classes in local tests.

Useful? React with 👍 / 👎.

parseMethod = DefaultHomeRemoteDataSource.class.getDeclaredMethod("parseResponse", JSONObject.class);
parseMethod.setAccessible(true);
}

@SuppressWarnings("unchecked")
private List<PromotedApp> invokeParse(JSONObject json) throws Exception {
return (List<PromotedApp>) parseMethod.invoke(dataSource, json);
}

@Test
public void parseResponseFiltersOwnPackages() throws Exception {
String json = "{\"data\":{\"apps\":[" +
"{\"name\":\"App1\",\"packageName\":\"com.example.app1\",\"iconLogo\":\"logo1\"}," +
"{\"name\":\"App2\",\"packageName\":\"com.d4rk.androidtutorials.sample\",\"iconLogo\":\"logo2\"}]}}";
List<PromotedApp> result = invokeParse(new JSONObject(json));
assertEquals(1, result.size());
assertEquals("com.example.app1", result.get(0).getPackageName());
}

@Test
public void parseResponseMalformedJsonReturnsEmptyList() throws Exception {
String json = "{\"data\":{}}";
List<PromotedApp> result = invokeParse(new JSONObject(json));
assertTrue(result.isEmpty());
}
}
Loading