Skip to content

Commit

Permalink
added file .json for unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
jedsada committed Apr 26, 2017
1 parent ed87f5f commit 1246d83
Show file tree
Hide file tree
Showing 7 changed files with 624 additions and 79 deletions.
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;
// }
}
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() {
//
// }
}
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;
}

}
Loading

0 comments on commit 1246d83

Please sign in to comment.