-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jedsada
committed
Apr 26, 2017
1 parent
ed87f5f
commit 1246d83
Showing
7 changed files
with
624 additions
and
79 deletions.
There are no files selected for viewing
66 changes: 27 additions & 39 deletions
66
app/src/androidTest/java/com/wisdomlanna/www/dagger2_mvp_example/main/BaseTestServer.java
This file contains 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 |
---|---|---|
@@ -1,45 +1,33 @@ | ||
package com.wisdomlanna.www.dagger2_mvp_example.main; | ||
|
||
|
||
import android.support.test.espresso.core.deps.guava.base.Charsets; | ||
import android.support.test.espresso.core.deps.guava.io.Resources; | ||
|
||
import com.wisdomlanna.www.dagger2_mvp_example.module.NetworkModule; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.mockwebserver.MockWebServer; | ||
|
||
abstract class BaseTestServer { | ||
|
||
private MockWebServer server; | ||
|
||
abstract void setBefore(); | ||
|
||
abstract void setAfter(); | ||
|
||
@Before | ||
public void startMockWebServer() throws IOException { | ||
server = new MockWebServer(); | ||
server.start(); | ||
NetworkModule.ENPOINT = server.url("/").toString(); | ||
setBefore(); | ||
} | ||
|
||
String getDataFromFile(String resource) throws IOException { | ||
return Resources.toString(Resources.getResource(resource), Charsets.UTF_8); | ||
} | ||
|
||
@After | ||
public void stopServer() throws Exception { | ||
server.shutdown(); | ||
setAfter(); | ||
} | ||
|
||
MockWebServer getServer() { | ||
return server; | ||
} | ||
// private MockWebServer server; | ||
// | ||
// abstract void setBefore(); | ||
// | ||
// abstract void setAfter(); | ||
// | ||
// @Before | ||
// public void startMockWebServer() throws IOException { | ||
// server = new MockWebServer(); | ||
// server.start(); | ||
// NetworkModule.ENPOINT = server.url("/").toString(); | ||
// setBefore(); | ||
// } | ||
// | ||
// String getDataFromFile(String resource) throws IOException { | ||
// return Resources.toString(Resources.getResource(resource), Charsets.UTF_8); | ||
// } | ||
// | ||
// @After | ||
// public void stopServer() throws Exception { | ||
// server.shutdown(); | ||
// setAfter(); | ||
// } | ||
// | ||
// MockWebServer getServer() { | ||
// return server; | ||
// } | ||
} |
65 changes: 25 additions & 40 deletions
65
app/src/androidTest/java/com/wisdomlanna/www/dagger2_mvp_example/main/MainActivityTest.java
This file contains 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 |
---|---|---|
@@ -1,51 +1,36 @@ | ||
package com.wisdomlanna.www.dagger2_mvp_example.main; | ||
|
||
|
||
import android.support.test.rule.ActivityTestRule; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import com.wisdomlanna.www.dagger2_mvp_example.R; | ||
import com.wisdomlanna.www.dagger2_mvp_example.ui.MainActivity; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.mockwebserver.MockResponse; | ||
|
||
import static android.support.test.espresso.Espresso.onView; | ||
import static android.support.test.espresso.assertion.ViewAssertions.matches; | ||
import static android.support.test.espresso.matcher.ViewMatchers.withId; | ||
import static android.support.test.espresso.matcher.ViewMatchers.withText; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class MainActivityTest extends BaseTestServer { | ||
|
||
@Rule | ||
public ActivityTestRule<MainActivity> mainActivity | ||
= new ActivityTestRule<>(MainActivity.class, true, false); | ||
|
||
@Test | ||
public void validateResultRetrofit() throws IOException { | ||
getServer().enqueue(new MockResponse() | ||
.setResponseCode(200) | ||
.setBody(getDataFromFile("user.json"))); | ||
|
||
mainActivity.launchActivity(null); | ||
|
||
onView(withId(R.id.tvUsername)) | ||
.check(matches(withText("Jedsada Tiwongvorakul"))); | ||
} | ||
|
||
@Override | ||
void setBefore() { | ||
|
||
} | ||
|
||
@Override | ||
void setAfter() { | ||
|
||
} | ||
// @Rule | ||
// public ActivityTestRule<MainActivity> mainActivity | ||
// = new ActivityTestRule<>(MainActivity.class, true, false); | ||
// | ||
// @Test | ||
// public void validateResultRetrofit() throws IOException { | ||
// getServer().enqueue(new MockResponse() | ||
// .setResponseCode(200) | ||
// .setBody(getDataFromFile("user.json"))); | ||
// | ||
// mainActivity.launchActivity(null); | ||
// | ||
// onView(withId(R.id.tvUsername)) | ||
// .check(matches(withText("Jedsada Tiwongvorakul"))); | ||
// } | ||
// | ||
// @Override | ||
// void setBefore() { | ||
// | ||
// } | ||
// | ||
// @Override | ||
// void setAfter() { | ||
// | ||
// } | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/test/java/com/wisdomlanna/www/dagger2_mvp_example/main/utils/JsonMockUtility.java
This file contains 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,42 @@ | ||
package com.wisdomlanna.www.dagger2_mvp_example.main.utils; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
public class JsonMockUtility { | ||
|
||
public <T> T getJsonToMock(String fileName, Class<T> className) { | ||
String json = getJsonFromResources(fileName); | ||
Gson gson = new GsonBuilder().create(); | ||
return gson.fromJson(json, className); | ||
} | ||
|
||
private String getJsonFromResources(String fileName) { | ||
BufferedReader reader = null; | ||
String mLineResult = ""; | ||
try { | ||
reader = new BufferedReader( | ||
new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(fileName), "UTF-8")); | ||
|
||
// do reading, usually loop until end of file reading | ||
String mLine = null; | ||
while ((mLine = reader.readLine()) != null) { | ||
mLineResult += mLine; | ||
} | ||
} catch (IOException ignored) { | ||
} finally { | ||
if (reader != null) { | ||
try { | ||
reader.close(); | ||
} catch (IOException ignored) { | ||
} | ||
} | ||
} | ||
return mLineResult; | ||
} | ||
|
||
} |
Oops, something went wrong.