├── .gitignore ├── LICENSE.txt ├── README.md ├── app ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src │ ├── androidTest │ └── java │ │ ├── com │ │ └── handstandsam │ │ │ └── httpmocking │ │ │ ├── tests │ │ │ ├── RealServerTest.java │ │ │ ├── mockwebserver │ │ │ │ ├── MockWebServerDispatcherTest.java │ │ │ │ └── MockWebServerEnqueueTest.java │ │ │ └── wiremock │ │ │ │ ├── WireMockActivityInstrumentationTestCase2.java │ │ │ │ ├── WireMockAndroidTest1.java │ │ │ │ ├── WireMockAndroidTest2.java │ │ │ │ └── WireMockApplicationTestCase.java │ │ │ └── util │ │ │ └── AssetReaderUtil.java │ │ └── wiremock │ │ ├── AndroidAssetsBinaryFile.java │ │ ├── AndroidAssetsReadOnlyFileSource.java │ │ └── WireMockApplicationTestCase2.java │ ├── debug │ ├── AndroidManifest.xml │ └── assets │ │ ├── body_files │ │ └── atlanta-conditions.json │ │ └── mappings │ │ └── hello.json │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── joshskeen │ │ └── weatherview │ │ ├── MainActivity.java │ │ ├── inject │ │ ├── InjectionFactory.java │ │ └── WeatherviewApplication.java │ │ ├── model │ │ ├── ConditionsServiceResponse.java │ │ └── WeatherCondition.java │ │ └── service │ │ ├── WeatherServiceInterface.java │ │ └── WeatherServiceManager.java │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ ├── layout │ └── activity_main.xml │ ├── menu │ └── menu_main.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | ## Android 7 | # Built application files 8 | *.apk 9 | *.ap_ 10 | 11 | # Files for the Dalvik VM 12 | *.dex 13 | 14 | # Java class files 15 | *.class 16 | 17 | # Generated files 18 | bin/ 19 | gen/ 20 | 21 | # Gradle files 22 | .gradle/ 23 | build/ 24 | 25 | # Local configuration file (sdk path, etc) 26 | local.properties 27 | 28 | # Proguard folder generated by Eclipse 29 | proguard/ 30 | 31 | # Log Files 32 | *.log 33 | 34 | ### AndroidStudio 35 | 36 | /*.iml 37 | app.iml 38 | *.iml 39 | 40 | ## Directory-based project format: 41 | .idea/ 42 | # if you remove the above rule, at least ignore the following: 43 | 44 | # User-specific stuff: 45 | #.idea/workspace.xml 46 | #.idea/tasks.xml 47 | #.idea/dictionaries 48 | 49 | # Sensitive or high-churn files: 50 | #.idea/dataSources.ids 51 | #.idea/dataSources.xml 52 | #.idea/sqlDataSources.xml 53 | #.idea/dynamic.xml 54 | #.idea/uiDesigner.xml 55 | 56 | # Gradle: 57 | #.idea/gradle.xml 58 | #.idea/libraries 59 | 60 | # Mongo Explorer plugin: 61 | #.idea/mongoSettings.xml 62 | 63 | ## File-based project format: 64 | *.ipr 65 | *.iws 66 | 67 | ## Plugin-specific files: 68 | 69 | # IntelliJ 70 | out/ 71 | 72 | # mpeltonen/sbt-idea plugin 73 | .idea_modules/ 74 | 75 | # JIRA plugin 76 | atlassian-ide-plugin.xml 77 | 78 | # Crashlytics plugin (for Android Studio and IntelliJ) 79 | com_crashlytics_export_strings.xml 80 | 81 | /build 82 | /captures 83 | 84 | spoon-output 85 | run_spoon.sh 86 | 87 | spoon-runner-1.5.7-jar-with-dependencies.jar 88 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## AndroidHttpMockingExamples ## 2 | 3 | **These are examples by @HandstandSam - https://handstandsam.com to allow you to mock out external HTTP services on and Android device itself.** 4 | 5 | ## Android "androidTest" Examples for: ## 6 | * WireMock 2.8.0 7 | * okhttp MockWebServer 3.8.0 8 | 9 | ### Links ### 10 | * WireMock (by @tomakehurst) - http://wiremock.org/ 11 | * WireMock on GitHub - https://github.com/tomakehurst/wiremock/ 12 | * okhttp MockWebServer on GitHub - https://github.com/square/okhttp/tree/master/mockwebserver 13 | 14 | ### Credits ### 15 | The Sample Application and Espresso Testing Logic is from the following repositories: 16 | * https://github.com/mutexkid/weatherview 17 | * https://github.com/mike011/Wiremock_Android_Example 18 | 19 | Thanks @yogurtearl for tips about how he got WireMock 1.x to work on Android. 20 | 21 | Thanks @abhagupta for your help in wrangling WireMock 2.1.x to work on Android. 22 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'spoon' 3 | 4 | android { 5 | compileSdkVersion 26 6 | buildToolsVersion '26.0.0' 7 | 8 | defaultConfig { 9 | applicationId "weatherview.joshskeen.com.weatherview" 10 | minSdkVersion 21 11 | targetSdkVersion 22 12 | buildConfigField "String", "WEATHERVIEW_API_KEY", "\"840dbdf2737a7ff9\""; 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | // Enabling Multidex support for 'androidTest' builds because of large dependencies 16 | multiDexEnabled = true 17 | } 18 | 19 | dexOptions { 20 | javaMaxHeapSize "4g" 21 | } 22 | 23 | buildTypes { 24 | debug { 25 | buildConfigField "String", "IP", "\"127.0.0.1\"" 26 | buildConfigField "Integer", "PORT", "9998" 27 | } 28 | } 29 | 30 | compileOptions { 31 | sourceCompatibility JavaVersion.VERSION_1_7 32 | targetCompatibility JavaVersion.VERSION_1_7 33 | } 34 | 35 | packagingOptions { 36 | exclude 'META-INF/maven/com.google.guava/guava/pom.properties' 37 | exclude 'META-INF/maven/com.google.guava/guava/pom.xml' 38 | } 39 | } 40 | 41 | dependencies { 42 | //------ Compile Dependencies ------ 43 | implementation('com.squareup.retrofit2:retrofit:2.3.0') { 44 | exclude group: 'com.squareup.okhttp3', module: 'okhttp' 45 | } 46 | implementation 'com.squareup.retrofit2:converter-gson:2.3.0' 47 | implementation 'com.squareup.okhttp3:okhttp:3.8.0' 48 | implementation "com.squareup.okhttp3:logging-interceptor:3.8.0" 49 | implementation "org.slf4j:slf4j-api:1.7.12" 50 | 51 | implementation 'com.google.dagger:dagger:2.8' 52 | 53 | //------ Android Test Dependencies ------ 54 | androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1') { 55 | exclude group: 'com.android.support', module: 'support-annotations' 56 | exclude group: 'com.google.guava', module: 'guava' 57 | exclude group: 'junit', module: 'junit' 58 | } 59 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 60 | 61 | androidTestImplementation 'junit:junit:4.12' 62 | 63 | //MockWebServer - Version 2.2 of mockwebserver doesn't work because of an issue, so forcing v2.1 - https://github.com/square/okhttp/issues/1069 64 | androidTestImplementation('com.squareup.okhttp3:mockwebserver:3.8.0') { 65 | exclude module: 'okhttp' 66 | } 67 | 68 | //WireMock 69 | androidTestImplementation("com.github.tomakehurst:wiremock:2.8.0") { 70 | //Using Android Version Instead 71 | exclude group: 'org.apache.httpcomponents', module: 'httpclient' 72 | 73 | //Version conflict with our app's slf4j version 74 | exclude group: 'org.slf4j', module: 'slf4j-api' 75 | 76 | //Was getting a classpath conflict for org.objectweb.asm.AnnotationVisitor which is a part of 'net.minidev:asm' 77 | exclude group: 'org.ow2.asm', module: 'asm' 78 | 79 | //Was getting this warning, so decided to ignore this version included by WireMock. 80 | //Warning:Dependency org.json:json:20090211 is ignored as it may be conflicting with the internal version provided by Android. 81 | //In case of problem, please repackage with jarjar to change the class packages 82 | exclude group: 'org.json', module: 'json' 83 | } 84 | androidTestImplementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1' 85 | androidTestImplementation 'commons-io:commons-io:2.5' 86 | 87 | androidTestImplementation "com.squareup.spoon:spoon-client:1.7.1" 88 | 89 | androidTestImplementation 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0' 90 | } 91 | configurations { 92 | all*.exclude group: 'commons-logging', module: 'commons-logging' 93 | all*.exclude group: 'com.android.support', module: 'support-annotations' 94 | } 95 | 96 | // This section is optional 97 | spoon { 98 | // for debug output 99 | debug = true 100 | 101 | codeCoverage = false 102 | 103 | // To grant permissions to Android M >= devices (For screenshot storage) 104 | grantAllPermissions = true 105 | } -------------------------------------------------------------------------------- /app/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handstandsam/AndroidHttpMockingExamples/929d8575ac0d22091b87bbc9d82d5785d5d1b0c6/app/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 21 11:34:03 PDT 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip 7 | -------------------------------------------------------------------------------- /app/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/handstandsam/httpmocking/tests/RealServerTest.java: -------------------------------------------------------------------------------- 1 | package com.handstandsam.httpmocking.tests; 2 | 3 | import android.support.test.espresso.Espresso; 4 | import android.support.test.espresso.IdlingResource; 5 | import android.support.test.espresso.matcher.ViewMatchers; 6 | import android.support.test.rule.ActivityTestRule; 7 | import android.support.test.runner.AndroidJUnit4; 8 | 9 | import com.jakewharton.espresso.OkHttp3IdlingResource; 10 | import com.joshskeen.weatherview.MainActivity; 11 | import com.joshskeen.weatherview.R; 12 | import com.joshskeen.weatherview.inject.InjectionFactory; 13 | 14 | import org.junit.After; 15 | import org.junit.Before; 16 | import org.junit.Rule; 17 | import org.junit.Test; 18 | import org.junit.runner.RunWith; 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | 22 | import static android.support.test.espresso.Espresso.onView; 23 | import static android.support.test.espresso.action.ViewActions.click; 24 | import static android.support.test.espresso.action.ViewActions.replaceText; 25 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 26 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 27 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 28 | import static org.hamcrest.Matchers.containsString; 29 | 30 | 31 | @RunWith(AndroidJUnit4.class) 32 | public class RealServerTest { 33 | 34 | Logger logger = LoggerFactory.getLogger(RealServerTest.class); 35 | 36 | @Rule 37 | public ActivityTestRule activityRule = new ActivityTestRule<>(MainActivity.class); 38 | 39 | 40 | IdlingResource idlingResource; 41 | 42 | @Before 43 | public void setUp() { 44 | idlingResource = 45 | OkHttp3IdlingResource.create("OkHttp", InjectionFactory.okHttpClient); 46 | Espresso.registerIdlingResources(idlingResource); 47 | } 48 | 49 | @After 50 | public void tearDown() { 51 | Espresso.unregisterIdlingResources(idlingResource); 52 | } 53 | 54 | /** 55 | * Test Real API 56 | */ 57 | @Test 58 | public void testRealAPI() { 59 | logger.debug("testRealAPI"); 60 | 61 | onView(ViewMatchers.withId(R.id.editText)).perform(replaceText("atlanta, ga")); 62 | onView(withId(R.id.button)).perform(click()); 63 | onView(withId(R.id.textView)).check(matches(withText(containsString("GA")))); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/handstandsam/httpmocking/tests/mockwebserver/MockWebServerDispatcherTest.java: -------------------------------------------------------------------------------- 1 | package com.handstandsam.httpmocking.tests.mockwebserver; 2 | 3 | import android.support.test.espresso.matcher.ViewMatchers; 4 | import android.support.test.rule.ActivityTestRule; 5 | 6 | import com.joshskeen.weatherview.BuildConfig; 7 | import com.joshskeen.weatherview.MainActivity; 8 | import com.joshskeen.weatherview.R; 9 | import com.joshskeen.weatherview.service.WeatherServiceManager; 10 | import com.squareup.spoon.Spoon; 11 | 12 | import org.junit.After; 13 | import org.junit.Before; 14 | import org.junit.Rule; 15 | import org.junit.Test; 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | import okhttp3.mockwebserver.Dispatcher; 20 | import okhttp3.mockwebserver.MockResponse; 21 | import okhttp3.mockwebserver.MockWebServer; 22 | import okhttp3.mockwebserver.RecordedRequest; 23 | 24 | import static android.support.test.espresso.Espresso.onView; 25 | import static android.support.test.espresso.action.ViewActions.click; 26 | import static android.support.test.espresso.action.ViewActions.replaceText; 27 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 28 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 29 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 30 | import static com.handstandsam.httpmocking.util.AssetReaderUtil.asset; 31 | import static org.hamcrest.Matchers.containsString; 32 | 33 | 34 | public class MockWebServerDispatcherTest { 35 | 36 | Logger logger = LoggerFactory.getLogger(MockWebServerDispatcherTest.class); 37 | 38 | final MockWebServer mMockWebServer = new MockWebServer(); 39 | 40 | private MainActivity activity; 41 | 42 | @Rule 43 | public ActivityTestRule activityRule = new ActivityTestRule<>(MainActivity.class); 44 | 45 | @Before 46 | public void setUp() throws Exception { 47 | mMockWebServer.start(BuildConfig.PORT); 48 | } 49 | 50 | @After 51 | public void tearDown() throws Exception { 52 | mMockWebServer.shutdown(); 53 | } 54 | 55 | /** 56 | * Test okhttp mockwebserver Dispatcher 57 | */ 58 | @Test 59 | public void testMockWebServerDispatcher() { 60 | activity = activityRule.getActivity(); 61 | logger.debug("testMockWebServerDispatcher"); 62 | 63 | //Use a dispatcher 64 | final Dispatcher dispatcher = new Dispatcher() { 65 | 66 | @Override 67 | public MockResponse dispatch(RecordedRequest request) throws InterruptedException { 68 | if (request.getPath().equals("/api/840dbdf2737a7ff9/conditions/q/CA/atlanta.json")) { 69 | String jsonBody = asset(activity, "atlanta-conditions.json"); 70 | return new MockResponse().setResponseCode(200).setBody(jsonBody); 71 | } 72 | return new MockResponse().setResponseCode(404); 73 | } 74 | }; 75 | mMockWebServer.setDispatcher(dispatcher); 76 | 77 | String okhttpMockWebServerUrl = mMockWebServer.url("/").toString(); 78 | logger.debug("okhttp mockserver URL: " + okhttpMockWebServerUrl); 79 | activity.setWeatherServiceManager(new WeatherServiceManager(okhttpMockWebServerUrl)); 80 | 81 | Spoon.screenshot(activity, "one"); 82 | onView(ViewMatchers.withId(R.id.editText)).perform(replaceText("atlanta")); 83 | onView(withId(R.id.button)).perform(click()); 84 | Spoon.screenshot(activity, "two"); 85 | onView(withId(R.id.textView)).check(matches(withText(containsString("GA")))); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/handstandsam/httpmocking/tests/mockwebserver/MockWebServerEnqueueTest.java: -------------------------------------------------------------------------------- 1 | package com.handstandsam.httpmocking.tests.mockwebserver; 2 | 3 | import android.support.test.espresso.matcher.ViewMatchers; 4 | import android.support.test.rule.ActivityTestRule; 5 | 6 | import com.handstandsam.httpmocking.util.AssetReaderUtil; 7 | import com.joshskeen.weatherview.BuildConfig; 8 | import com.joshskeen.weatherview.MainActivity; 9 | import com.joshskeen.weatherview.R; 10 | import com.joshskeen.weatherview.service.WeatherServiceManager; 11 | 12 | import org.junit.After; 13 | import org.junit.Before; 14 | import org.junit.Rule; 15 | import org.junit.Test; 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | import okhttp3.mockwebserver.MockResponse; 20 | import okhttp3.mockwebserver.MockWebServer; 21 | 22 | import static android.support.test.espresso.Espresso.onView; 23 | import static android.support.test.espresso.action.ViewActions.click; 24 | import static android.support.test.espresso.action.ViewActions.replaceText; 25 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 26 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 27 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 28 | import static org.hamcrest.Matchers.containsString; 29 | 30 | public class MockWebServerEnqueueTest { 31 | 32 | Logger logger = LoggerFactory.getLogger(MockWebServerEnqueueTest.class); 33 | 34 | final MockWebServer mMockWebServer = new MockWebServer(); 35 | 36 | @Rule 37 | public ActivityTestRule activityRule = new ActivityTestRule<>(MainActivity.class); 38 | 39 | private MainActivity activity; 40 | 41 | @Before 42 | public void setUp() throws Exception { 43 | activity = activityRule.getActivity(); 44 | mMockWebServer.start(BuildConfig.PORT); 45 | //script MockWebServer to return this JSON 46 | String assetJson = AssetReaderUtil.asset(activity, "atlanta-conditions.json"); 47 | mMockWebServer.enqueue(new MockResponse().setBody(assetJson)); 48 | 49 | String okhttpMockWebServerUrl = mMockWebServer.url("/").toString(); 50 | logger.debug("okhttp mockserver URL: " + okhttpMockWebServerUrl); 51 | 52 | String serviceEndpoint = "http://127.0.0.1:" + BuildConfig.PORT; 53 | logger.debug("MockWebServer Endpoint: " + serviceEndpoint); 54 | activity.setWeatherServiceManager(new WeatherServiceManager(serviceEndpoint)); 55 | 56 | } 57 | 58 | @After 59 | public void tearDown() throws Exception { 60 | mMockWebServer.shutdown(); 61 | } 62 | 63 | /** 64 | * Test okhttp mockwebserver Enqueue 65 | */ 66 | @Test 67 | public void testMockWebServerEnqueue() { 68 | activity = activityRule.getActivity(); 69 | logger.debug("testMockWebServerEnqueue"); 70 | 71 | onView(ViewMatchers.withId(R.id.editText)).perform(replaceText("atlanta")); 72 | onView(withId(R.id.button)).perform(click()); 73 | onView(withId(R.id.textView)).check(matches(withText(containsString("GA")))); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/handstandsam/httpmocking/tests/wiremock/WireMockActivityInstrumentationTestCase2.java: -------------------------------------------------------------------------------- 1 | package com.handstandsam.httpmocking.tests.wiremock; 2 | 3 | import android.support.test.espresso.matcher.ViewMatchers; 4 | import android.support.test.rule.ActivityTestRule; 5 | 6 | import com.github.tomakehurst.wiremock.junit.WireMockRule; 7 | import com.joshskeen.weatherview.BuildConfig; 8 | import com.joshskeen.weatherview.MainActivity; 9 | import com.joshskeen.weatherview.R; 10 | import com.joshskeen.weatherview.service.WeatherServiceManager; 11 | 12 | import org.junit.Rule; 13 | import org.junit.Test; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | 17 | import static android.support.test.espresso.Espresso.onView; 18 | import static android.support.test.espresso.action.ViewActions.click; 19 | import static android.support.test.espresso.action.ViewActions.replaceText; 20 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 21 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 22 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 23 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; 24 | import static com.github.tomakehurst.wiremock.client.WireMock.get; 25 | import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; 26 | import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; 27 | import static com.handstandsam.httpmocking.util.AssetReaderUtil.asset; 28 | import static org.hamcrest.Matchers.containsString; 29 | 30 | public class WireMockActivityInstrumentationTestCase2 { 31 | 32 | Logger logger = LoggerFactory.getLogger(WireMockActivityInstrumentationTestCase2.class); 33 | 34 | private MainActivity activity; 35 | 36 | @Rule 37 | public WireMockRule wireMockRule = new WireMockRule(BuildConfig.PORT); 38 | 39 | @Rule 40 | public ActivityTestRule activityRule = new ActivityTestRule<>(MainActivity.class); 41 | 42 | /** 43 | * Test WireMock 44 | */ 45 | @Test 46 | public void testWiremock() { 47 | activity = activityRule.getActivity(); 48 | String jsonBody = asset(activity, "atlanta-conditions.json"); 49 | stubFor(get(urlMatching("/api/.*")) 50 | .willReturn(aResponse() 51 | .withStatus(200) 52 | .withBody(jsonBody))); 53 | 54 | String serviceEndpoint = "http://127.0.0.1:" + BuildConfig.PORT; 55 | logger.debug("WireMock Endpoint: " + serviceEndpoint); 56 | activity.setWeatherServiceManager(new WeatherServiceManager(serviceEndpoint)); 57 | 58 | onView(ViewMatchers.withId(R.id.editText)).perform(replaceText("atlanta")); 59 | onView(withId(R.id.button)).perform(click()); 60 | onView(withId(R.id.textView)).check(matches(withText(containsString("GA")))); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/handstandsam/httpmocking/tests/wiremock/WireMockAndroidTest1.java: -------------------------------------------------------------------------------- 1 | package com.handstandsam.httpmocking.tests.wiremock; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.test.InstrumentationRegistry; 6 | import android.support.test.espresso.matcher.ViewMatchers; 7 | import android.support.test.rule.ActivityTestRule; 8 | 9 | import com.github.tomakehurst.wiremock.junit.WireMockRule; 10 | import com.joshskeen.weatherview.BuildConfig; 11 | import com.joshskeen.weatherview.MainActivity; 12 | import com.joshskeen.weatherview.R; 13 | import com.joshskeen.weatherview.service.WeatherServiceManager; 14 | 15 | import org.junit.Rule; 16 | import org.junit.Test; 17 | import org.slf4j.Logger; 18 | import org.slf4j.LoggerFactory; 19 | 20 | import static android.support.test.espresso.Espresso.onView; 21 | import static android.support.test.espresso.action.ViewActions.click; 22 | import static android.support.test.espresso.action.ViewActions.replaceText; 23 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 24 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 25 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 26 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; 27 | import static com.github.tomakehurst.wiremock.client.WireMock.get; 28 | import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; 29 | import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; 30 | import static com.handstandsam.httpmocking.util.AssetReaderUtil.asset; 31 | import static org.hamcrest.Matchers.containsString; 32 | 33 | public class WireMockAndroidTest1 { 34 | 35 | Logger logger = LoggerFactory.getLogger(WireMockAndroidTest1.class); 36 | 37 | private MainActivity activity; 38 | 39 | @Rule 40 | public ActivityTestRule activityRule = new ActivityTestRule( 41 | MainActivity.class, 42 | true, // initialTouchMode 43 | false); // launchActivity. False to set intent per method 44 | 45 | @Rule 46 | public WireMockRule wireMockRule = new WireMockRule(BuildConfig.PORT); 47 | 48 | /** 49 | * Test WireMock 50 | */ 51 | @Test 52 | public void testWiremock() { 53 | Context applicationContext = InstrumentationRegistry.getTargetContext().getApplicationContext(); 54 | activity = activityRule.launchActivity(new Intent(applicationContext, MainActivity.class)); 55 | String jsonBody = asset(activity, "atlanta-conditions.json"); 56 | stubFor(get(urlMatching("/api/.*")) 57 | .willReturn(aResponse() 58 | .withStatus(200) 59 | .withBody(jsonBody))); 60 | 61 | String serviceEndpoint = "http://127.0.0.1:" + BuildConfig.PORT; 62 | logger.debug("WireMock Endpoint: " + serviceEndpoint); 63 | activity.setWeatherServiceManager(new WeatherServiceManager(serviceEndpoint)); 64 | 65 | onView(ViewMatchers.withId(R.id.editText)).perform(replaceText("atlanta")); 66 | onView(withId(R.id.button)).perform(click()); 67 | onView(withId(R.id.textView)).check(matches(withText(containsString("GA")))); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/handstandsam/httpmocking/tests/wiremock/WireMockAndroidTest2.java: -------------------------------------------------------------------------------- 1 | package com.handstandsam.httpmocking.tests.wiremock; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.test.InstrumentationRegistry; 6 | import android.support.test.espresso.matcher.ViewMatchers; 7 | import android.support.test.rule.ActivityTestRule; 8 | 9 | import com.github.tomakehurst.wiremock.junit.WireMockRule; 10 | import com.joshskeen.weatherview.BuildConfig; 11 | import com.joshskeen.weatherview.MainActivity; 12 | import com.joshskeen.weatherview.R; 13 | import com.joshskeen.weatherview.service.WeatherServiceManager; 14 | 15 | import org.junit.Rule; 16 | import org.junit.Test; 17 | import org.slf4j.Logger; 18 | import org.slf4j.LoggerFactory; 19 | 20 | import static android.support.test.espresso.Espresso.onView; 21 | import static android.support.test.espresso.action.ViewActions.click; 22 | import static android.support.test.espresso.action.ViewActions.replaceText; 23 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 24 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 25 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 26 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; 27 | import static com.github.tomakehurst.wiremock.client.WireMock.get; 28 | import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; 29 | import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; 30 | import static com.handstandsam.httpmocking.util.AssetReaderUtil.asset; 31 | import static org.hamcrest.Matchers.containsString; 32 | 33 | public class WireMockAndroidTest2 { 34 | 35 | Logger logger = LoggerFactory.getLogger(WireMockAndroidTest2.class); 36 | 37 | private MainActivity activity; 38 | 39 | @Rule 40 | public ActivityTestRule activityRule = new ActivityTestRule( 41 | MainActivity.class, 42 | true, // initialTouchMode 43 | false); // launchActivity. False to set intent per method 44 | 45 | @Rule 46 | public WireMockRule wireMockRule = new WireMockRule(BuildConfig.PORT); 47 | 48 | /** 49 | * Test WireMock 50 | */ 51 | @Test 52 | public void testWiremock() { 53 | Context applicationContext = InstrumentationRegistry.getTargetContext().getApplicationContext(); 54 | activity = activityRule.launchActivity(new Intent(applicationContext, MainActivity.class)); 55 | String jsonBody = asset(activity, "atlanta-conditions.json"); 56 | stubFor(get(urlMatching("/api/.*")) 57 | .willReturn(aResponse() 58 | .withStatus(200) 59 | .withBody(jsonBody))); 60 | 61 | String serviceEndpoint = "http://127.0.0.1:" + BuildConfig.PORT; 62 | logger.debug("WireMock Endpoint: " + serviceEndpoint); 63 | activity.setWeatherServiceManager(new WeatherServiceManager(serviceEndpoint)); 64 | 65 | onView(ViewMatchers.withId(R.id.editText)).perform(replaceText("atlanta")); 66 | onView(withId(R.id.button)).perform(click()); 67 | onView(withId(R.id.textView)).check(matches(withText(containsString("GA")))); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/handstandsam/httpmocking/tests/wiremock/WireMockApplicationTestCase.java: -------------------------------------------------------------------------------- 1 | package com.handstandsam.httpmocking.tests.wiremock; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | 6 | import com.github.tomakehurst.wiremock.WireMockServer; 7 | import com.joshskeen.weatherview.BuildConfig; 8 | 9 | import org.junit.After; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | 15 | import java.io.IOException; 16 | 17 | import okhttp3.OkHttpClient; 18 | import okhttp3.Request; 19 | import okhttp3.Response; 20 | 21 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; 22 | import static com.github.tomakehurst.wiremock.client.WireMock.get; 23 | import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; 24 | import static com.handstandsam.httpmocking.util.AssetReaderUtil.asset; 25 | import static org.junit.Assert.assertEquals; 26 | import static org.junit.Assert.assertFalse; 27 | 28 | public class WireMockApplicationTestCase { 29 | 30 | Logger logger = LoggerFactory.getLogger(WireMockApplicationTestCase.class); 31 | 32 | private Context applicationContext; 33 | 34 | /** 35 | * The @Rule - WireMockRule does NOT currently work for the ApplicationTestCase because it is not based on JUnit3 and not JUnit4 so we need to create & manage the WireMockServer ourselves 36 | *

37 | * As of 09.09.2015 - "To test an Android application object on the Android runtime you use the ApplicationTestCase class. 38 | * It is expected that Google will soon provide a special JUnit4 rule for testing the application object but at the moment his is not yet available." 39 | *

40 | * Reference: http://www.vogella.com/tutorials/AndroidTesting/article.html 41 | */ 42 | WireMockServer wireMockServer = new WireMockServer(BuildConfig.PORT); 43 | 44 | @Before 45 | public void setUp() { 46 | applicationContext = InstrumentationRegistry.getTargetContext().getApplicationContext(); 47 | wireMockServer.start(); 48 | } 49 | 50 | @After 51 | public void tearDown() throws Exception { 52 | wireMockServer.stop(); 53 | } 54 | 55 | /** 56 | * Test WireMock, but just the Http Call. Make sure the response matches the mock we want. 57 | */ 58 | @Test 59 | public void testWiremockPlusOkHttp() throws IOException { 60 | logger.debug("testWiremockPlusOkHttp"); 61 | 62 | String uri = "/api/840dbdf2737a7ff9/conditions/q/CA/atlanta.json"; 63 | 64 | String jsonBody = asset(applicationContext, "atlanta-conditions.json"); 65 | assertFalse(jsonBody.isEmpty()); 66 | wireMockServer.stubFor(get(urlMatching(uri)) 67 | .willReturn(aResponse() 68 | .withStatus(200) 69 | .withBody(jsonBody))); 70 | 71 | String serviceEndpoint = "http://127.0.0.1:" + BuildConfig.PORT; 72 | logger.debug("WireMock Endpoint: " + serviceEndpoint); 73 | 74 | OkHttpClient okHttpClient = new OkHttpClient(); 75 | Request request = new Request.Builder() 76 | .url(serviceEndpoint + uri) 77 | .build(); 78 | 79 | Response response = okHttpClient.newCall(request).execute(); 80 | 81 | assertEquals(jsonBody, response.body().string()); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/handstandsam/httpmocking/util/AssetReaderUtil.java: -------------------------------------------------------------------------------- 1 | package com.handstandsam.httpmocking.util; 2 | 3 | import android.content.Context; 4 | 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.InputStreamReader; 8 | 9 | public class AssetReaderUtil { 10 | 11 | public static String asset(Context context, String assetPath) { 12 | try { 13 | StringBuilder buf = new StringBuilder(); 14 | InputStream inputStream = context.getAssets().open("body_files/" + assetPath); 15 | return inputStreamToString(inputStream, "UTF-8"); 16 | } catch (IOException e) { 17 | throw new RuntimeException(e); 18 | } 19 | } 20 | 21 | private static final int BUFFER_SIZE = 4 * 1024; 22 | 23 | public static String inputStreamToString(InputStream inputStream, String charsetName) 24 | throws IOException { 25 | StringBuilder builder = new StringBuilder(); 26 | InputStreamReader reader = new InputStreamReader(inputStream, charsetName); 27 | char[] buffer = new char[BUFFER_SIZE]; 28 | int length; 29 | while ((length = reader.read(buffer)) != -1) { 30 | builder.append(buffer, 0, length); 31 | } 32 | return builder.toString(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/androidTest/java/wiremock/AndroidAssetsBinaryFile.java: -------------------------------------------------------------------------------- 1 | package wiremock; 2 | 3 | import android.content.res.AssetManager; 4 | 5 | import com.github.tomakehurst.wiremock.common.BinaryFile; 6 | import com.github.tomakehurst.wiremock.common.TextFile; 7 | 8 | import org.apache.commons.io.IOUtils; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | 13 | public class AndroidAssetsBinaryFile extends TextFile { 14 | 15 | private final AssetManager assetManager; 16 | 17 | private String assetPath; 18 | 19 | public AndroidAssetsBinaryFile(AssetManager assetManager, String assetPath) { 20 | super(null); // We're completely overriding the super() implementation 21 | this.assetManager = assetManager; 22 | this.assetPath = assetPath; 23 | } 24 | 25 | public byte[] readContents() { 26 | try { 27 | InputStream inputStream = assetManager.open(assetPath); 28 | return IOUtils.toByteArray(inputStream); 29 | } catch (IOException e) { 30 | e.printStackTrace(); 31 | } 32 | return null; 33 | } 34 | 35 | // private void closeStream(InputStream stream) { 36 | // if (stream != null) { 37 | // try { 38 | // stream.close(); 39 | // } catch (IOException var3) { 40 | // throw new RuntimeException(var3); 41 | // } 42 | // } 43 | // } 44 | 45 | public String name() { 46 | return assetPath; 47 | } 48 | 49 | public String toString() { 50 | return this.name(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /app/src/androidTest/java/wiremock/AndroidAssetsReadOnlyFileSource.java: -------------------------------------------------------------------------------- 1 | package wiremock; 2 | 3 | import android.content.res.AssetManager; 4 | 5 | import com.github.tomakehurst.wiremock.common.BinaryFile; 6 | import com.github.tomakehurst.wiremock.common.FileSource; 7 | import com.github.tomakehurst.wiremock.common.TextFile; 8 | 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | import java.io.IOException; 13 | import java.io.InputStream; 14 | import java.net.URI; 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | /** 19 | * WARNING: This is currently be developed and not working yet. 20 | */ 21 | public class AndroidAssetsReadOnlyFileSource implements FileSource { 22 | 23 | Logger logger = LoggerFactory.getLogger(AndroidAssetsReadOnlyFileSource.class); 24 | 25 | private final AssetManager assetManager; 26 | 27 | private String assetPath; 28 | 29 | public AndroidAssetsReadOnlyFileSource(AssetManager assetManager) { 30 | this(assetManager, ""); 31 | } 32 | 33 | public AndroidAssetsReadOnlyFileSource(AssetManager assetManager, String assetPath) { 34 | this.assetManager = assetManager; 35 | this.assetPath = assetPath; 36 | } 37 | 38 | @Override 39 | public BinaryFile getBinaryFileNamed(String name) { 40 | return new AndroidAssetsBinaryFile(assetManager, assetPath + "/" + name); 41 | } 42 | 43 | @Override 44 | public TextFile getTextFileNamed(String s) { 45 | return null; 46 | } 47 | 48 | @Override 49 | public void createIfNecessary() { 50 | return; 51 | } 52 | 53 | @Override 54 | public FileSource child(String name) { 55 | return new AndroidAssetsReadOnlyFileSource(assetManager, name); 56 | } 57 | 58 | @Override 59 | public String getPath() { 60 | return assetPath; 61 | } 62 | 63 | @Override 64 | public URI getUri() { 65 | return null; 66 | } 67 | 68 | @Override 69 | public List listFilesRecursively() { 70 | logger.debug("list files recursively"); 71 | try { 72 | String[] nameList = assetManager.list(assetPath); 73 | List textFiles = new ArrayList<>(); 74 | for (String name : nameList) { 75 | TextFile textFile = new AndroidAssetsBinaryFile(assetManager, assetPath + "/" + name); 76 | textFiles.add(textFile); 77 | } 78 | return textFiles; 79 | } catch (IOException e) { 80 | e.printStackTrace(); 81 | } 82 | return null; 83 | } 84 | 85 | @Override 86 | public void writeTextFile(String name, String contents) { 87 | return; 88 | } 89 | 90 | @Override 91 | public void writeBinaryFile(String name, byte[] contents) { 92 | return; 93 | } 94 | 95 | @Override 96 | public boolean exists() { 97 | boolean exists = false; 98 | InputStream inputStream = null; 99 | try { 100 | inputStream = assetManager.open(assetPath); 101 | exists = true; 102 | } catch (IOException e) { 103 | e.printStackTrace(); 104 | 105 | try { 106 | String[] strings = assetManager.list(assetPath); 107 | logger.debug(strings.toString()); 108 | exists = true; 109 | } catch (IOException e1) { 110 | e1.printStackTrace(); 111 | } 112 | } finally { 113 | if (inputStream != null) { 114 | try { 115 | inputStream.close(); 116 | } catch (IOException e) { 117 | e.printStackTrace(); 118 | } 119 | } 120 | } 121 | return exists; 122 | } 123 | 124 | @Override 125 | public void deleteFile(String s) { 126 | 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /app/src/androidTest/java/wiremock/WireMockApplicationTestCase2.java: -------------------------------------------------------------------------------- 1 | package wiremock; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.rule.ActivityTestRule; 6 | 7 | import com.github.tomakehurst.wiremock.WireMockServer; 8 | import com.joshskeen.weatherview.BuildConfig; 9 | import com.joshskeen.weatherview.MainActivity; 10 | import com.joshskeen.weatherview.inject.InjectionFactory; 11 | 12 | import org.junit.After; 13 | import org.junit.Before; 14 | import org.junit.Rule; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | 18 | import java.io.IOException; 19 | 20 | import okhttp3.OkHttpClient; 21 | import okhttp3.Request; 22 | import okhttp3.Response; 23 | 24 | import static org.junit.Assert.assertEquals; 25 | 26 | public class WireMockApplicationTestCase2 { 27 | 28 | Logger logger = LoggerFactory.getLogger(WireMockApplicationTestCase2.class); 29 | 30 | /** 31 | * The @Rule - WireMockRule does NOT currently work for the ApplicationTestCase because it is not based on JUnit3 and not JUnit4 so we need to create & manage the WireMockServer ourselves 32 | *

33 | * As of 09.09.2015 - "To test an Android application object on the Android runtime you use the ApplicationTestCase class. 34 | * It is expected that Google will soon provide a special JUnit4 rule for testing the application object but at the moment his is not yet available." 35 | *

36 | * Reference: http://www.vogella.com/tutorials/AndroidTesting/article.html 37 | */ 38 | WireMockServer wireMockServer; 39 | 40 | 41 | @Rule 42 | public ActivityTestRule activityRule = new ActivityTestRule<>(MainActivity.class); 43 | 44 | private Context androidTestContext; 45 | private Context applicationContext; 46 | 47 | 48 | @Before 49 | protected void setUp() throws Exception { 50 | this.androidTestContext = InstrumentationRegistry.getContext(); 51 | this.applicationContext = InstrumentationRegistry.getTargetContext().getApplicationContext(); 52 | wireMockServer = new WireMockServer(8080, new AndroidAssetsReadOnlyFileSource(androidTestContext.getAssets()), false); 53 | wireMockServer.start(); 54 | } 55 | 56 | @After 57 | protected void tearDown() throws Exception { 58 | wireMockServer.stop(); 59 | } 60 | 61 | /** 62 | * Test WireMock, but just the Http Call. Make sure the response matches the mock we want. 63 | */ 64 | public void testWiremockPlusOkHttp() throws IOException { 65 | logger.debug("testWiremockPlusOkHttp"); 66 | 67 | String serviceEndpoint = "http://127.0.0.1:" + BuildConfig.PORT; 68 | logger.debug("WireMock Endpoint: " + serviceEndpoint); 69 | 70 | OkHttpClient okHttpClient = InjectionFactory.okHttpClient; 71 | 72 | String uri = "/hello-world"; 73 | Request request = new Request.Builder() 74 | .url(serviceEndpoint + uri) 75 | .build(); 76 | 77 | Response response = okHttpClient.newCall(request).execute(); 78 | 79 | String responseBodyStr = response.body().string(); 80 | 81 | logger.debug("Response Body: " + responseBodyStr); 82 | assertEquals("hello world", responseBodyStr); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /app/src/debug/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/debug/assets/body_files/atlanta-conditions.json: -------------------------------------------------------------------------------- 1 | { 2 | "response": { 3 | "version":"0.1", 4 | "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", 5 | "features": { 6 | "conditions": 1 7 | } 8 | , "results": [ 9 | { 10 | "name": "Atlanta", 11 | "city": "Atlanta", 12 | "state": "GA", 13 | "country": "US", 14 | "country_iso3166":"US", 15 | "country_name":"USA", 16 | "zmw": "30301.1.99999", 17 | "l": "/q/zmw:30301.1.99999" 18 | } 19 | 20 | ] 21 | } 22 | } -------------------------------------------------------------------------------- /app/src/debug/assets/mappings/hello.json: -------------------------------------------------------------------------------- 1 | { 2 | "request": { 3 | "method": "GET", 4 | "url": "/hello-world" 5 | }, 6 | "response": { 7 | "status": 200, 8 | "body": "hello world", 9 | "headers": { 10 | "Content-Type": "text/plain" 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/joshskeen/weatherview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.joshskeen.weatherview; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.os.StrictMode; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.EditText; 9 | import android.widget.TextView; 10 | 11 | import com.joshskeen.weatherview.model.ConditionsServiceResponse; 12 | import com.joshskeen.weatherview.model.WeatherCondition; 13 | import com.joshskeen.weatherview.service.WeatherServiceManager; 14 | 15 | import java.util.List; 16 | 17 | import retrofit2.Call; 18 | import retrofit2.Callback; 19 | import retrofit2.Response; 20 | 21 | 22 | public class MainActivity extends Activity implements View.OnClickListener { 23 | protected EditText text; 24 | protected Button button; 25 | 26 | @Override 27 | public void onClick(View v) { 28 | text.setText(""); 29 | } 30 | 31 | WeatherServiceManager weatherServiceManager = new WeatherServiceManager("http://api.wunderground.com/"); 32 | 33 | public void setWeatherServiceManager(WeatherServiceManager mWeatherServiceManager) { 34 | this.weatherServiceManager = mWeatherServiceManager; 35 | } 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 40 | StrictMode.setThreadPolicy(policy); 41 | 42 | super.onCreate(savedInstanceState); 43 | 44 | setContentView(R.layout.activity_main); 45 | 46 | button = (Button) findViewById(R.id.button); 47 | text = (EditText) findViewById(R.id.editText); 48 | text.setOnClickListener(this); 49 | 50 | button.setOnClickListener( 51 | new View.OnClickListener() { 52 | public void onClick(View view) { 53 | String location = text.getText().toString(); 54 | weatherServiceManager.getConditionsFor(location).enqueue(new Callback() { 55 | @Override 56 | public void onResponse(Call call, Response response) { 57 | List conditionsForCity = response.body().getConditionsResponse().getWeatherConditions(); 58 | if (conditionsForCity != null) { 59 | StringBuffer out = new StringBuffer(); 60 | for (WeatherCondition wc : conditionsForCity) { 61 | out.append(wc.toString()); 62 | } 63 | 64 | TextView outText = (TextView) findViewById(R.id.textView); 65 | outText.setText(out.toString()); 66 | } 67 | } 68 | 69 | @Override 70 | public void onFailure(Call call, Throwable t) { 71 | 72 | } 73 | }); 74 | 75 | 76 | } 77 | }); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /app/src/main/java/com/joshskeen/weatherview/inject/InjectionFactory.java: -------------------------------------------------------------------------------- 1 | package com.joshskeen.weatherview.inject; 2 | 3 | import com.joshskeen.weatherview.BuildConfig; 4 | 5 | import okhttp3.Interceptor; 6 | import okhttp3.OkHttpClient; 7 | import okhttp3.logging.HttpLoggingInterceptor; 8 | import retrofit2.Retrofit; 9 | import retrofit2.converter.gson.GsonConverterFactory; 10 | 11 | public class InjectionFactory { 12 | 13 | public static OkHttpClient okHttpClient; 14 | 15 | static { 16 | Interceptor loggingInterceptor = new HttpLoggingInterceptor() 17 | .setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE); 18 | 19 | okHttpClient = new OkHttpClient.Builder() 20 | .addNetworkInterceptor(loggingInterceptor) 21 | .build(); 22 | } 23 | 24 | /** 25 | * Build Retrofit 2 26 | */ 27 | public static Retrofit buildRetrofit(final String endpoint) { 28 | return new Retrofit.Builder() 29 | .addConverterFactory(GsonConverterFactory.create()) 30 | .baseUrl(endpoint) 31 | .client(okHttpClient) 32 | .build(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/joshskeen/weatherview/inject/WeatherviewApplication.java: -------------------------------------------------------------------------------- 1 | package com.joshskeen.weatherview.inject; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | public class WeatherviewApplication extends Application { 7 | 8 | 9 | @Override 10 | public void onCreate() { 11 | super.onCreate(); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/joshskeen/weatherview/model/ConditionsServiceResponse.java: -------------------------------------------------------------------------------- 1 | package com.joshskeen.weatherview.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | public class ConditionsServiceResponse { 8 | 9 | public class ConditionsResponse { 10 | @SerializedName("results") 11 | public List mWeatherConditions; 12 | 13 | public List getWeatherConditions() { 14 | return mWeatherConditions; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "ConditionsResponse{" + 20 | "mWeatherConditions=" + mWeatherConditions + 21 | '}'; 22 | } 23 | } 24 | 25 | @SerializedName("response") 26 | public ConditionsResponse mConditionsResponse; 27 | 28 | public ConditionsResponse getConditionsResponse() { 29 | return mConditionsResponse; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "ConditionsServiceResponse{" + 35 | "mConditionsResponse=" + mConditionsResponse + 36 | '}'; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/joshskeen/weatherview/model/WeatherCondition.java: -------------------------------------------------------------------------------- 1 | package com.joshskeen.weatherview.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | public class WeatherCondition { 6 | 7 | @SerializedName("name") 8 | public String mName; 9 | 10 | @SerializedName("city") 11 | public String mCity; 12 | 13 | @SerializedName("state") 14 | public String mState; 15 | 16 | @SerializedName("country") 17 | public String mCountry; 18 | 19 | @Override 20 | public String toString() { 21 | return "WeatherCondition{" + 22 | "mName='" + mName + '\'' + 23 | ", mCity='" + mCity + '\'' + 24 | ", mState='" + mState + '\'' + 25 | ", mCountry='" + mCountry + '\'' + 26 | '}'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/joshskeen/weatherview/service/WeatherServiceInterface.java: -------------------------------------------------------------------------------- 1 | package com.joshskeen.weatherview.service; 2 | 3 | 4 | import com.joshskeen.weatherview.BuildConfig; 5 | import com.joshskeen.weatherview.model.ConditionsServiceResponse; 6 | 7 | import retrofit2.Call; 8 | import retrofit2.http.GET; 9 | import retrofit2.http.Path; 10 | 11 | 12 | public interface WeatherServiceInterface { 13 | 14 | @GET("/api/" + BuildConfig.WEATHERVIEW_API_KEY + "/conditions/q/CA/{location}.json") 15 | public Call getConditions(@Path("location") String location); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/joshskeen/weatherview/service/WeatherServiceManager.java: -------------------------------------------------------------------------------- 1 | package com.joshskeen.weatherview.service; 2 | 3 | import com.joshskeen.weatherview.inject.InjectionFactory; 4 | import com.joshskeen.weatherview.model.ConditionsServiceResponse; 5 | 6 | import retrofit2.Call; 7 | 8 | public class WeatherServiceManager { 9 | 10 | private final WeatherServiceInterface mWeatherServiceInterface; 11 | 12 | public WeatherServiceManager(String weatherServiceEndpoint) { 13 | mWeatherServiceInterface = InjectionFactory.buildRetrofit(weatherServiceEndpoint) 14 | .create(WeatherServiceInterface.class); 15 | } 16 | 17 | public Call getConditionsFor(String name) { 18 | return mWeatherServiceInterface.getConditions(name); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handstandsam/AndroidHttpMockingExamples/929d8575ac0d22091b87bbc9d82d5785d5d1b0c6/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handstandsam/AndroidHttpMockingExamples/929d8575ac0d22091b87bbc9d82d5785d5d1b0c6/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handstandsam/AndroidHttpMockingExamples/929d8575ac0d22091b87bbc9d82d5785d5d1b0c6/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/handstandsam/AndroidHttpMockingExamples/929d8575ac0d22091b87bbc9d82d5785d5d1b0c6/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 18 | 19 | 28 | 29 |