-
-
Notifications
You must be signed in to change notification settings - Fork 3
test: add remote data source parsing tests #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
MihaiCristianCondrea
wants to merge
3
commits into
main
from
codex/add-unit-tests-for-defaulthomeremotedatasource
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...test/java/com/d4rk/androidtutorials/java/data/source/DefaultHomeRemoteDataSourceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| 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()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
DefaultHomeRemoteDataSourcein a local JVM test. Instantiating that class callsnew Handler(Looper.getMainLooper()), but the JVM test environment ships only the stubbedandroid.os.Looperfromandroid.jar. When./gradlew testruns with a valid SDK, the call toLooper.getMainLooper()will throwRuntimeException: 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 instrumentationandroidTestsource set, or theDefaultHomeRemoteDataSourceshould be refactored to avoid framework classes in local tests.Useful? React with 👍 / 👎.