-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started with mock server, increased gradle speed, GenericApp supports…
… test variant.
- Loading branch information
Showing
13 changed files
with
277 additions
and
7 deletions.
There are no files selected for viewing
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
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
30 changes: 30 additions & 0 deletions
30
sampleApp/src/androidTest/java/com/zeyad/usecases/app/MockWebServerRule.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,30 @@ | ||
package com.zeyad.usecases.app; | ||
|
||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
import okhttp3.internal.tls.SslClient; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
|
||
/** | ||
* @author by ZIaDo on 6/15/17. | ||
*/ | ||
|
||
public class MockWebServerRule implements TestRule { | ||
public final MockWebServer server = new MockWebServer(); | ||
|
||
@Override | ||
public Statement apply(final Statement base, Description description) { | ||
return new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
server.useHttps(SslClient.localhost().socketFactory, false); | ||
server.start(); | ||
base.evaluate(); | ||
server.shutdown(); | ||
} | ||
}; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
sampleApp/src/androidTest/java/com/zeyad/usecases/app/OkHttpIdlingResourceRule.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,32 @@ | ||
package com.zeyad.usecases.app; | ||
|
||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.espresso.Espresso; | ||
import android.support.test.espresso.IdlingResource; | ||
|
||
import com.jakewharton.espresso.OkHttp3IdlingResource; | ||
|
||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
/** | ||
* @author by ZIaDo on 6/15/17. | ||
*/ | ||
public class OkHttpIdlingResourceRule implements TestRule { | ||
@Override | ||
public Statement apply(final Statement base, Description description) { | ||
TestGenericApplication app = (TestGenericApplication) | ||
InstrumentationRegistry.getTargetContext().getApplicationContext(); | ||
return new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
IdlingResource idlingResource = OkHttp3IdlingResource.create( | ||
"okhttp", app.getOkHttpBuilder().build()); | ||
Espresso.registerIdlingResources(idlingResource); | ||
base.evaluate(); | ||
Espresso.unregisterIdlingResources(idlingResource); | ||
} | ||
}; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
sampleApp/src/androidTest/java/com/zeyad/usecases/app/TestGenericApplication.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,31 @@ | ||
package com.zeyad.usecases.app; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import javax.net.ssl.SSLSocketFactory; | ||
import javax.net.ssl.X509TrustManager; | ||
|
||
import io.appflate.restmock.RESTMockServer; | ||
import okhttp3.internal.tls.SslClient; | ||
|
||
/** | ||
* @author by ZIaDo on 6/15/17. | ||
*/ | ||
|
||
public class TestGenericApplication extends GenericApplication { | ||
@NonNull | ||
@Override | ||
public String getApiBaseUrl() { | ||
return RESTMockServer.getUrl(); | ||
} | ||
|
||
@Override | ||
X509TrustManager getX509TrustManager() { | ||
return SslClient.localhost().trustManager; | ||
} | ||
|
||
@Override | ||
SSLSocketFactory getSSlSocketFactory() { | ||
return SslClient.localhost().socketFactory; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
sampleApp/src/androidTest/java/com/zeyad/usecases/app/UseCasesTestRunner.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,17 @@ | ||
package com.zeyad.usecases.app; | ||
|
||
import android.app.Application; | ||
import android.content.Context; | ||
|
||
import io.appflate.restmock.android.RESTMockTestRunner; | ||
|
||
/** | ||
* @author by ZIaDo on 6/15/17. | ||
*/ | ||
public class UseCasesTestRunner extends RESTMockTestRunner { | ||
@Override | ||
public Application newApplication(ClassLoader cl, String className, Context context) | ||
throws InstantiationException, IllegalAccessException, ClassNotFoundException { | ||
return super.newApplication(cl, TestGenericApplication.class.getName(), context); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
sampleApp/src/androidTest/java/com/zeyad/usecases/app/assets/users/userList.json
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,4 @@ | ||
{ | ||
"login": "octocat", | ||
"followers": 1500 | ||
} |
86 changes: 86 additions & 0 deletions
86
...p/src/androidTest/java/com/zeyad/usecases/app/screens/user/list/UserListActivityTest.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,86 @@ | ||
package com.zeyad.usecases.app.screens.user.list; | ||
|
||
import android.support.test.rule.ActivityTestRule; | ||
|
||
import com.zeyad.usecases.app.OkHttpIdlingResourceRule; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.appflate.restmock.RESTMockServer; | ||
import io.appflate.restmock.RequestsVerifier; | ||
import okhttp3.mockwebserver.MockResponse; | ||
|
||
import static io.appflate.restmock.utils.RequestMatchers.pathEndsWith; | ||
import static io.appflate.restmock.utils.RequestMatchers.pathStartsWith; | ||
|
||
/** | ||
* @author by ZIaDo on 6/15/17. | ||
*/ | ||
public class UserListActivityTest { | ||
private static final String USER_LIST_BODY = "{ \"login\" : \"octocat\", \"followers\" : 1500 }"; | ||
private final String urlPart = "users?since=0"; | ||
@Rule | ||
public ActivityTestRule<UserListActivity> activityRule | ||
= new ActivityTestRule<>(UserListActivity.class, true, false); | ||
|
||
@Rule | ||
public OkHttpIdlingResourceRule okHttpIdlingResourceRule = new OkHttpIdlingResourceRule(); | ||
|
||
// @Rule | ||
// public MockWebServerRule mockWebServerRule = new MockWebServerRule(); | ||
|
||
@Before | ||
public void before() { | ||
RESTMockServer.reset(); | ||
} | ||
|
||
@Test | ||
public void followers() throws IOException, InterruptedException { | ||
RESTMockServer.whenGET(pathEndsWith(urlPart)) | ||
.thenReturnFile("users/userList.json"); | ||
|
||
activityRule.launchActivity(null); | ||
|
||
// onView(withId(R.id.followers)) | ||
// .check(matches(withText("1500"))); | ||
|
||
RequestsVerifier.verifyGET(pathStartsWith("/" + urlPart)).invoked(); | ||
} | ||
|
||
@Test | ||
public void status404() throws IOException { | ||
RESTMockServer.whenGET(pathEndsWith(urlPart)) | ||
.thenReturnEmpty(404); | ||
|
||
activityRule.launchActivity(null); | ||
|
||
// onView(withId(R.id.followers)) | ||
// .check(matches(withText("404"))); | ||
} | ||
|
||
@Test | ||
public void malformedJson() throws IOException { | ||
RESTMockServer.whenGET(pathEndsWith(urlPart)).thenReturn(new MockResponse().setBody("Jason")); | ||
|
||
activityRule.launchActivity(null); | ||
|
||
// onView(withId(R.id.followers)) | ||
// .check(matches(withText("IOException"))); | ||
} | ||
|
||
@Test | ||
public void timeout() throws IOException { | ||
RESTMockServer.whenGET(pathEndsWith(urlPart)).thenReturn( | ||
new MockResponse().setBody(USER_LIST_BODY).throttleBody(1, 1, TimeUnit.SECONDS)); | ||
|
||
activityRule.launchActivity(null); | ||
|
||
// onView(withId(R.id.followers)) | ||
// .check(matches(withText("SocketTimeoutException"))); | ||
} | ||
} |
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
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
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
File renamed without changes.
Oops, something went wrong.