diff --git a/README.md b/README.md index 8900204..bdc3250 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Add the dependency ```groovy dependencies { - androidTestCompile 'com.github.andrzejchm.RESTMock:android:${LATEST_VERSION}' + androidTestImplementation 'com.github.andrzejchm.RESTMock:android:${LATEST_VERSION}' // see "Releases" tab for latest version } ``` diff --git a/androidsample/build.gradle b/androidsample/build.gradle index a684924..4dbd995 100644 --- a/androidsample/build.gradle +++ b/androidsample/build.gradle @@ -32,6 +32,8 @@ android { versionCode 1 versionName "1.0" testInstrumentationRunner "io.appflate.restmock.androidsample.CustomTestRunner" + + testInstrumentationRunnerArguments = ["notPackage": "org.bouncycastle.pqc.crypto.qtesla"] } buildTypes { release { @@ -49,7 +51,7 @@ android { configurations.all { resolutionStrategy { force "androidx.annotation:annotation:1.0.2" - force "com.squareup.okhttp3:okhttp:${okHttpVersion}" + force "com.squareup.okhttp3:okhttp:$okHttpVersion" } } @@ -58,27 +60,27 @@ dependencies { implementation "androidx.appcompat:appcompat:1.1.0" implementation "com.google.android.material:material:1.1.0" //noinspection AnnotationProcessorOnCompilePath - implementation "com.jakewharton:butterknife:${butterKnifeVersion}" - annotationProcessor "com.jakewharton:butterknife-compiler:${butterKnifeVersion}" - implementation "com.google.dagger:dagger:${daggerVersion}" - annotationProcessor "com.google.dagger:dagger-compiler:${daggerVersion}" + implementation "com.jakewharton:butterknife:$butterKnifeVersion" + annotationProcessor "com.jakewharton:butterknife-compiler:$butterKnifeVersion" + implementation "com.google.dagger:dagger:$daggerVersion" + annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion" implementation "com.squareup.retrofit2:retrofit:2.7.2" implementation "com.squareup.retrofit2:converter-gson:2.7.2" - implementation "com.squareup.okhttp3:logging-interceptor:${okHttpVersion}" - implementation "com.squareup.okhttp3:okhttp:${okHttpVersion}" + implementation "com.squareup.okhttp3:logging-interceptor:$okHttpVersion" + implementation "com.squareup.okhttp3:okhttp:$okHttpVersion" implementation "com.google.code.gson:gson:2.8.5" implementation "com.google.code.findbugs:annotations:2.0.3" compileOnly "javax.annotation:jsr250-api:1.0" //Test dependencies - androidTestImplementation "androidx.test:rules:${androidXTestVersion}" - androidTestImplementation "androidx.test:runner:${androidXTestVersion}" + androidTestImplementation "androidx.test:rules:$androidXTestVersion" + androidTestImplementation "androidx.test:runner:$androidXTestVersion" - androidTestImplementation "androidx.test.espresso:espresso-core:${espressoVersion}" + androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion" //this dependency tends to break things for most users. Im adding it to check whether the app compiles correctly - androidTestImplementation("androidx.test.espresso:espresso-contrib:${espressoVersion}") + androidTestImplementation("androidx.test.espresso:espresso-contrib:$espressoVersion") androidTestImplementation "androidx.annotation:annotation:1.1.0" //the most important, RESTMock! :) @@ -87,8 +89,8 @@ dependencies { testImplementation "androidx.appcompat:appcompat:1.1.0" testImplementation "com.google.android.material:material:1.1.0" testImplementation "junit:junit:4.13" - testImplementation "androidx.test.espresso:espresso-core:${espressoVersion}" - testImplementation("androidx.test.espresso:espresso-contrib:${espressoVersion}") + testImplementation "androidx.test.espresso:espresso-core:$espressoVersion" + testImplementation("androidx.test.espresso:espresso-contrib:$espressoVersion") testImplementation project(":android") testImplementation(project(":core")) } diff --git a/build.gradle b/build.gradle index cbae93d..e845b61 100644 --- a/build.gradle +++ b/build.gradle @@ -21,18 +21,18 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.6.1' + classpath 'com.android.tools.build:gradle:3.6.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' } } ext { - libraryVersion = "0.4.2" + libraryVersion = "0.4.3" compileSdk = 29 targetSdk = 29 minSdk = 21 buildTools = "29.0.2" - okHttpVersion = "4.4.1" + okHttpVersion = "4.6.0" butterKnifeVersion = "10.2.1" espressoVersion = "3.1.0" androidXTestVersion = "1.2.0" diff --git a/core/src/test/java/io/appflate/restmock/RequestVerifierTest.java b/core/src/test/java/io/appflate/restmock/RequestVerifierTest.java index c0296ae..99782d1 100644 --- a/core/src/test/java/io/appflate/restmock/RequestVerifierTest.java +++ b/core/src/test/java/io/appflate/restmock/RequestVerifierTest.java @@ -16,17 +16,6 @@ package io.appflate.restmock; -import io.appflate.restmock.exceptions.RequestInvocationCountMismatchException; -import io.appflate.restmock.exceptions.RequestInvocationCountNotEnoughException; -import io.appflate.restmock.exceptions.RequestNotInvokedException; -import io.appflate.restmock.utils.RequestMatchers; -import io.appflate.restmock.utils.TestUtils; -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Locale; -import okhttp3.mockwebserver.RecordedRequest; import org.hamcrest.Matcher; import org.junit.After; import org.junit.Before; @@ -34,6 +23,20 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Locale; +import java.util.Objects; + +import io.appflate.restmock.exceptions.RequestInvocationCountMismatchException; +import io.appflate.restmock.exceptions.RequestInvocationCountNotEnoughException; +import io.appflate.restmock.exceptions.RequestNotInvokedException; +import io.appflate.restmock.utils.RequestMatchers; +import io.appflate.restmock.utils.TestUtils; +import okhttp3.mockwebserver.RecordedRequest; + import static io.appflate.restmock.RequestsVerifier.verifyDELETE; import static io.appflate.restmock.RequestsVerifier.verifyGET; import static io.appflate.restmock.RequestsVerifier.verifyPOST; @@ -54,14 +57,13 @@ public class RequestVerifierTest { private static final String path = "sample"; private static final Matcher INVOKED_MATCHER = pathEndsWith(path); private static final Matcher NOT_INVOKED_MATCHER = pathEndsWith("else"); - static RESTMockFileParser fileParser; private final boolean useHttps; @Parameterized.Parameters(name = "useHttps={0}") public static Collection data() { - return Arrays.asList(new Object[] { - true, false + return Arrays.asList(new Object[]{ + true, false }); } @@ -71,7 +73,7 @@ public RequestVerifierTest(boolean useHttps) { @Before public void setup() { - fileParser = mock(RESTMockFileParser.class); + RESTMockFileParser fileParser = mock(RESTMockFileParser.class); RESTMockServerStarter.startSync(fileParser, new RESTMockOptions.Builder().useHttps(useHttps).build()); RESTMockServer.dispatcher = spy(RESTMockServer.dispatcher); } @@ -130,9 +132,9 @@ public void takesLastNumOfElementsWhenHistoryNotEmpty() throws Exception { List recordedRequests = RequestsVerifier.takeLast(3); assertEquals(3, recordedRequests.size()); - assertEquals("POST", recordedRequests.get(0).getMethod().toUpperCase(Locale.US)); - assertEquals("DELETE", recordedRequests.get(1).getMethod().toUpperCase(Locale.US)); - assertEquals("HEAD", recordedRequests.get(2).getMethod().toUpperCase(Locale.US)); + assertEquals("POST", Objects.requireNonNull(recordedRequests.get(0).getMethod()).toUpperCase(Locale.US)); + assertEquals("DELETE", Objects.requireNonNull(recordedRequests.get(1).getMethod()).toUpperCase(Locale.US)); + assertEquals("HEAD", Objects.requireNonNull(recordedRequests.get(2).getMethod()).toUpperCase(Locale.US)); } @Test @@ -145,10 +147,10 @@ public void takeLastNumOfElementsExceedsHistorySizeTakesWholeHistory() throws Ex List recordedRequests = RequestsVerifier.takeLast(10); assertEquals(4, recordedRequests.size()); - assertEquals("GET", recordedRequests.get(0).getMethod().toUpperCase(Locale.US)); - assertEquals("POST", recordedRequests.get(1).getMethod().toUpperCase(Locale.US)); - assertEquals("DELETE", recordedRequests.get(2).getMethod().toUpperCase(Locale.US)); - assertEquals("HEAD", recordedRequests.get(3).getMethod().toUpperCase(Locale.US)); + assertEquals("GET", Objects.requireNonNull(recordedRequests.get(0).getMethod()).toUpperCase(Locale.US)); + assertEquals("POST", Objects.requireNonNull(recordedRequests.get(1).getMethod()).toUpperCase(Locale.US)); + assertEquals("DELETE", Objects.requireNonNull(recordedRequests.get(2).getMethod()).toUpperCase(Locale.US)); + assertEquals("HEAD", Objects.requireNonNull(recordedRequests.get(3).getMethod()).toUpperCase(Locale.US)); } @Test(expected = IllegalArgumentException.class) @@ -172,9 +174,9 @@ public void takesFirstNumOfElementsWhenHistoryNotEmpty() throws Exception { List recordedRequests = RequestsVerifier.takeFirst(3); assertEquals(3, recordedRequests.size()); - assertEquals("GET", recordedRequests.get(0).getMethod().toUpperCase(Locale.US)); - assertEquals("POST", recordedRequests.get(1).getMethod().toUpperCase(Locale.US)); - assertEquals("DELETE", recordedRequests.get(2).getMethod().toUpperCase(Locale.US)); + assertEquals("GET", Objects.requireNonNull(recordedRequests.get(0).getMethod()).toUpperCase(Locale.US)); + assertEquals("POST", Objects.requireNonNull(recordedRequests.get(1).getMethod()).toUpperCase(Locale.US)); + assertEquals("DELETE", Objects.requireNonNull(recordedRequests.get(2).getMethod()).toUpperCase(Locale.US)); } @Test @@ -187,10 +189,10 @@ public void takeFirstNumOfElementsExceedsHistorySizeTakesAllHistory() throws Exc List recordedRequests = RequestsVerifier.takeFirst(10); assertEquals(4, recordedRequests.size()); - assertEquals("GET", recordedRequests.get(0).getMethod().toUpperCase(Locale.US)); - assertEquals("POST", recordedRequests.get(1).getMethod().toUpperCase(Locale.US)); - assertEquals("DELETE", recordedRequests.get(2).getMethod().toUpperCase(Locale.US)); - assertEquals("HEAD", recordedRequests.get(3).getMethod().toUpperCase(Locale.US)); + assertEquals("GET", Objects.requireNonNull(recordedRequests.get(0).getMethod()).toUpperCase(Locale.US)); + assertEquals("POST", Objects.requireNonNull(recordedRequests.get(1).getMethod()).toUpperCase(Locale.US)); + assertEquals("DELETE", Objects.requireNonNull(recordedRequests.get(2).getMethod()).toUpperCase(Locale.US)); + assertEquals("HEAD", Objects.requireNonNull(recordedRequests.get(3).getMethod()).toUpperCase(Locale.US)); } @Test @@ -203,9 +205,9 @@ public void takesSubsetOfRequests() throws Exception { List recordedRequests = RequestsVerifier.take(1, 4); assertEquals(3, recordedRequests.size()); - assertEquals("POST", recordedRequests.get(0).getMethod().toUpperCase(Locale.US)); - assertEquals("DELETE", recordedRequests.get(1).getMethod().toUpperCase(Locale.US)); - assertEquals("HEAD", recordedRequests.get(2).getMethod().toUpperCase(Locale.US)); + assertEquals("POST", Objects.requireNonNull(recordedRequests.get(0).getMethod()).toUpperCase(Locale.US)); + assertEquals("DELETE", Objects.requireNonNull(recordedRequests.get(1).getMethod()).toUpperCase(Locale.US)); + assertEquals("HEAD", Objects.requireNonNull(recordedRequests.get(2).getMethod()).toUpperCase(Locale.US)); } @Test(expected = IllegalArgumentException.class) @@ -230,9 +232,9 @@ public void takeMatchingFindsAllRelevantRequests() throws Exception { TestUtils.get(path); List recordedRequests = RequestsVerifier.takeAllMatching(RequestMatchers.isGET()); assertEquals(3, recordedRequests.size()); - assertEquals("GET", recordedRequests.get(0).getMethod().toUpperCase(Locale.US)); - assertEquals("GET", recordedRequests.get(1).getMethod().toUpperCase(Locale.US)); - assertEquals("GET", recordedRequests.get(2).getMethod().toUpperCase(Locale.US)); + assertEquals("GET", Objects.requireNonNull(recordedRequests.get(0).getMethod()).toUpperCase(Locale.US)); + assertEquals("GET", Objects.requireNonNull(recordedRequests.get(1).getMethod()).toUpperCase(Locale.US)); + assertEquals("GET", Objects.requireNonNull(recordedRequests.get(2).getMethod()).toUpperCase(Locale.US)); } @Test(expected = IllegalArgumentException.class) @@ -247,13 +249,13 @@ public void takeFirstNumOfElementsWithInvalidCountThrowsException() throws Excep } @Test - public void takeSingleFirstWhenNoHistoryIsNull() throws Exception { + public void takeSingleFirstWhenNoHistoryIsNull() { RESTMockServer.whenRequested(pathEndsWith(path)).thenReturnString("a single call"); assertNull(RequestsVerifier.takeFirst()); } @Test - public void takeSingleLastWhenNoHistoryIsNull() throws Exception { + public void takeSingleLastWhenNoHistoryIsNull() { RESTMockServer.whenRequested(pathEndsWith(path)).thenReturnString("a single call"); assertNull(RequestsVerifier.takeLast()); } @@ -275,7 +277,7 @@ public void testReset() throws Exception { } @Test(expected = RequestNotInvokedException.class) - public void testInvoked_NotInvokedException() throws Exception { + public void testInvoked_NotInvokedException() { verifyRequest(NOT_INVOKED_MATCHER).invoked(); } @@ -298,7 +300,7 @@ public void testAtLeast_InvocationCountMismatchException() throws Exception { } @Test(expected = RequestNotInvokedException.class) - public void testExactly_NotInvokedException() throws Exception { + public void testExactly_NotInvokedException() { verifyRequest(NOT_INVOKED_MATCHER).exactly(3); } diff --git a/core/src/test/java/io/appflate/restmock/RequestsChainTest.java b/core/src/test/java/io/appflate/restmock/RequestsChainTest.java index ac4f2c7..c9f52b0 100644 --- a/core/src/test/java/io/appflate/restmock/RequestsChainTest.java +++ b/core/src/test/java/io/appflate/restmock/RequestsChainTest.java @@ -16,7 +16,6 @@ package io.appflate.restmock; -import org.hamcrest.Matcher; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -26,12 +25,12 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Objects; import java.util.concurrent.TimeUnit; import io.appflate.restmock.utils.TestUtils; import okhttp3.Response; import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; import static io.appflate.restmock.utils.RequestMatchers.pathEndsWith; import static org.junit.Assert.assertEquals; @@ -46,9 +45,7 @@ public class RequestsChainTest { private static final String path = "sample"; - private static final Matcher INVOKED_MATCHER = pathEndsWith(path); - static RESTMockFileParser fileParser; private final boolean useHttps; @Parameterized.Parameters(name = "useHttps={0}") @@ -64,7 +61,7 @@ public RequestsChainTest(boolean useHttps) { @Before public void setup() { - fileParser = mock(RESTMockFileParser.class); + RESTMockFileParser fileParser = mock(RESTMockFileParser.class); RESTMockServerStarter.startSync(fileParser, new RESTMockOptions.Builder().useHttps(useHttps).build()); RESTMockServer.dispatcher = spy(RESTMockServer.dispatcher); } @@ -87,13 +84,13 @@ public void multipleResponsesReturnedOneByOne() throws Exception { .thenReturnString("answer no 2") .thenReturnString("answer no 3"); - String response = TestUtils.get(path).body().string(); + String response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("a single call", response); - response = TestUtils.get(path).body().string(); + response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("answer no 2", response); - response = TestUtils.get(path).body().string(); + response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("answer no 3", response); } @@ -101,13 +98,13 @@ public void multipleResponsesReturnedOneByOne() throws Exception { public void multipleResponsesByVarargsReturnedOneByOne() throws Exception { RESTMockServer.whenGET(pathEndsWith(path)).thenReturnString("a single call", "answer no 2", "answer no 3"); - String response = TestUtils.get(path).body().string(); + String response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("a single call", response); - response = TestUtils.get(path).body().string(); + response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("answer no 2", response); - response = TestUtils.get(path).body().string(); + response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("answer no 3", response); } @@ -118,10 +115,10 @@ public void exceedingNumberOfDefinedResponsesReturnsLastOne() throws Exception { TestUtils.get(path); - String response = TestUtils.get(path).body().string(); + String response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals(lastResponse, response); - response = TestUtils.get(path).body().string(); + response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals(lastResponse, response); } @@ -136,19 +133,19 @@ public void multipleResponsesWithDifferentDelays() throws Exception { .delayBody(TimeUnit.MILLISECONDS, 500); long a = System.currentTimeMillis(); - String response = TestUtils.get(path).body().string(); + String response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("a single call", response); long b = System.currentTimeMillis(); assertEquals(1000, b - a, 100); a = System.currentTimeMillis(); - response = TestUtils.get(path).body().string(); + response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("answer no 2", response); b = System.currentTimeMillis(); assertEquals(100, b - a, 100); a = System.currentTimeMillis(); - response = TestUtils.get(path).body().string(); + response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("answer no 3", response); b = System.currentTimeMillis(); assertEquals(500, b - a, 100); @@ -163,19 +160,19 @@ public void multipleResponsesWithOneDelay() throws Exception { .thenReturnString("answer no 3"); long a = System.currentTimeMillis(); - String response = TestUtils.get(path).body().string(); + String response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("a single call", response); long b = System.currentTimeMillis(); assertEquals(300, b - a, 100); a = System.currentTimeMillis(); - response = TestUtils.get(path).body().string(); + response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("answer no 2", response); b = System.currentTimeMillis(); assertEquals(300, b - a, 100); a = System.currentTimeMillis(); - response = TestUtils.get(path).body().string(); + response = Objects.requireNonNull(TestUtils.get(path).body()).string(); assertEquals("answer no 3", response); b = System.currentTimeMillis(); assertEquals(300, b - a, 100); @@ -193,12 +190,12 @@ public void delaysBodyNotResponse() throws Exception { Response response = TestUtils.get(path); assertEquals(200, response.code()); long b = System.currentTimeMillis(); - assertEquals(0, b - a, 100); + assertEquals(0, b - a, 200); - String body = response.body().string(); + String body = Objects.requireNonNull(response.body()).string(); assertNotNull(body); long c = System.currentTimeMillis(); - assertEquals(500, c - b, 100); + assertEquals(500, c - b, 200); } @Test diff --git a/core/src/test/java/io/appflate/restmock/utils/TestUtils.java b/core/src/test/java/io/appflate/restmock/utils/TestUtils.java index 8c2b7f6..84142d8 100644 --- a/core/src/test/java/io/appflate/restmock/utils/TestUtils.java +++ b/core/src/test/java/io/appflate/restmock/utils/TestUtils.java @@ -16,16 +16,18 @@ package io.appflate.restmock.utils; -import io.appflate.restmock.RESTMockServer; import java.io.IOException; +import java.util.Collections; import java.util.Map; + +import io.appflate.restmock.RESTMockServer; import okhttp3.MediaType; import okhttp3.OkHttpClient; +import okhttp3.Protocol; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; -import static okhttp3.tls.internal.TlsUtil.localhost; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -36,7 +38,10 @@ public class TestUtils { private static final RequestBody EMPTY_JSON_BODY = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), ""); private static OkHttpClient okHttpClient = - new OkHttpClient.Builder().sslSocketFactory(RESTMockServer.getSSLSocketFactory(), RESTMockServer.getTrustManager()).build(); + new OkHttpClient.Builder() + .sslSocketFactory(RESTMockServer.getSSLSocketFactory(), RESTMockServer.getTrustManager()) + .protocols(Collections.singletonList(Protocol.HTTP_1_1)) + .build(); public static Response get(String path) throws IOException { path = normalizePath(path);