标签: mockwebserver

在Android上模拟服务器结果(Wiremock,MockWebServer等)

是否有人在记录服务器响应方面有完整的,最新的教程或项目-根据需要获取,发布和标头,以及使用Wiremock和/或模拟Web服务器播放它们?

我已经看了很多

android android-espresso mockwebserver wiremock

5
推荐指数
1
解决办法
674
查看次数

如何使用MockWebServer验证POST Body的内容?

我有几个使用Squares OkHttp的单元测试MockWebServer.测试运行得很好,一切都很好.到目前为止,我无法做的一件事是验证POST请求的内容.

我为什么要那样做?
我正在开发的REST Api有一些方法,需要在POST请求体中发送数据对象,其他方法要求将对象作为FormUrlEncoded数据字段发送.所以我想确保Retrofit接口设置正确.对规格.

以下单元测试将通过,但第一个测试将在正文内部错误地发送数据:

//Incorrect
@POST("api/v1/user/senddata")
Observable<Void> senddata (
        @Path("VIN") String vin,
        @Body PoiWrapper wrappedPoi);

//Correct
@FormUrlEncoded
@POST("api/v1/user/senddata")
Observable<Void> senddata(
        @Path("VIN") String vin,
        @Field("data") PoiWrapper wrappedPoi);
Run Code Online (Sandbox Code Playgroud)

我知道MockWebServertakeRequest()方法,但我坚持从该请求体获取实际的字段和数据.

谢谢您的帮助!

junit junit4 okhttp mockwebserver retrofit2

5
推荐指数
1
解决办法
3149
查看次数

如何在使用MockWebServer时使用Dagger 2.0支持多个端点

对于Android项目,我使用Dagger 2.6配置了Retrofit 2.1.0和OkHttp 3.4.1,如下面的Dagger模块所示.我的目标是使用@Named限定符支持多个后端.

@Module
public class ApiModule {

    private static final String GITHUB_QUALIFIER = "GitHub";

    private static final String TWITTER_QUALIFIER = "Twitter";

    @Provides
    GitHubClient provideGitHubClient(@Named(GITHUB_QUALIFIER) Retrofit retrofit) { /* ... */ }

    @Provides
    TwitterClient provideTwitterClient(@Named(TWITTER_QUALIFIER) Retrofit retrofit) { /* ... */ }

    @Named(GITHUB_QUALIFIER)
    @Provides
    @Singleton
    HttpUrl provideGitHubBaseUrl() { /* ... */ }

    @Named(TWITTER_QUALIFIER)
    @Provides
    @Singleton
    HttpUrl provideTwitterBaseUrl() { /* ... */ }

    @Named(GITHUB_QUALIFIER)
    @Provides
    @Singleton
    Retrofit getGitHubRetrofit(@Named(GITHUB_QUALIFIER) HttpUrl httpUrl, 
                               OkHttpClient okHttpClient) { /* ... */ }

    @Named(TWITTER_QUALIFIER) …
Run Code Online (Sandbox Code Playgroud)

android mockwebserver dagger-2 retrofit2 okhttp3

5
推荐指数
1
解决办法
523
查看次数

MockWebServer在Gradle构建中引发错误

我的gradle文件中的此项:

androidTestCompile ('com.squareup.okhttp:mockwebserver:2.7.0')
Run Code Online (Sandbox Code Playgroud)

抛出错误:

Warning:Conflict with dependency 'com.squareup.okio:okio'. Resolved versions for app (1.8.0) and test app (1.6.0) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Run Code Online (Sandbox Code Playgroud)

我尝试注释掉gradle文件中的不同编译项,以找出哪个冲突,但是我找不到使用com.squareup.okio:okio的哪个。

更新:我能够通过运行获取依赖项:gradlew.bat app:dependencies> c:\ tmp \ output.txt

+--- com.squareup.retrofit2:retrofit:2.0.0 -> 2.1.0
|    \--- com.squareup.okhttp3:okhttp:3.3.0
|         \--- com.squareup.okio:okio:1.8.0


--- com.squareup.okhttp:mockwebserver:2.7.0
|    +--- com.squareup.okhttp:okhttp:2.7.0
|    |    \--- com.squareup.okio:okio:1.6.0
Run Code Online (Sandbox Code Playgroud)

如您所见,改造2.0使用okhttp3,而okhttp3使用okio:1.8.0。另一方面,mockwebserver:2.7.0使用okhttp:2.7.0,后者使用okio:1.6.0。那么我该如何解决呢?

这是我的gradle文件的“ dependencies”部分中的条目:

compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:24.2.1'


        //retrofit
        compile 'com.squareup.retrofit2:retrofit:2.0.0'
        compile 'com.squareup.retrofit2:converter-gson:2.+'
        compile 'com.squareup.retrofit2:adapter-rxjava:2.+'
        compile 'com.squareup.retrofit2:retrofit-mock:2.+'



        //recycler view
        compile 'com.android.support:recyclerview-v7:+'

        //picasso image caching
        compile 'com.squareup.picasso:picasso:2.5.2'


        //jackson parser
        compile ( …
Run Code Online (Sandbox Code Playgroud)

android conflicting-libraries gradle mockwebserver

5
推荐指数
1
解决办法
1281
查看次数

为什么要使用MockWebServer而不是WireMock?

每当我需要模拟一些http请求时,我的第一个选择就是WireMock(我认为这实际上是标准的),但是今天我发现了一些替代工具-MockWebServer。

WireMockMockWebServer的优缺点是什么?

java mocking mockwebserver wiremock

5
推荐指数
1
解决办法
2486
查看次数

使用 Retrofit 具有动态 URL 的 OkHttp MockWebServer

我的应用程序使用动态 URL 进行 Web 服务调用(在 Android 上)。baseUrl设置为空,我们在服务接口中传递Retrofit2 @Url参数:

public interface UserService {  
    @GET
    public Call<ResponseBody> profilePicture(@Url String url);
}
Run Code Online (Sandbox Code Playgroud)

我们事先不知道主机/域,因此 MockWebServer 无法拦截请求。获取动态 URL 初始列表的调用是在不同的屏幕中进行的。一个想法是创建一种新风格,为要使用的 URL 提供本地数据源,这是我的后备计划。

我很好奇 MockWebServer 是否有任何其他方法来帮助测试此类情况并且可以仅限于测试代码。

android retrofit okhttp mockwebserver retrofit2

5
推荐指数
1
解决办法
3420
查看次数

如何模拟 JUnit 测试的 okhttp 响应

我正在通过 okhttp 向第 3 方 API 发出出站 HTTP 请求:

public @Nullable result Call3rdParty {
    OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
        .readTimeout(RW_TIMEOUT, TimeUnit.MILLISECONDS)
        .retryOnConnectionFailure(true)
        .build();
    

    Request request = new Request.Builder()
       .url(url)
       .build();
    Response response = client.newCall(request).execute();

    //Deserialize and do minor data manipulation...
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个单元测试并模拟响应。

  private MockWebServer server;

  @Before
  public void setUp() throws IOException {
    this.server = new MockWebServer();
    this.server.start();
  }

  @After
  public void tearDown() throws IOException {
    this.server.shutdown();
  }

  @Test
  public void Test_SUCCESS() throws Exception {
    String json = readFileAsString(file); …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mockito okhttp mockwebserver

5
推荐指数
1
解决办法
8217
查看次数

如何使用 OkHttpClient MockWebSever 返回图像/字节 []?

我正在开发一个 android 库项目,我需要从 REST 服务下载 PNG 图像,将其转换为 aBitmap并使用此库将其返回给应用程序。所以,我们有一个 REST web 服务,它返回一个 png 图像的字节数。我们使用Retrofitwith调用此服务rxJava。在我下面accessRemoteComm.getImage()的代码中,在 Observable 上进行了从 aResponseBodyBitmapa 内部的加载和转换.map。图像在应用程序中加载良好。我现在想对该方法进行单元测试,但我很难MockWebServer首先交付图像。在OnError不断调用:

java.lang.RuntimeException: Method decodeStream in android.graphics.BitmapFactory not mocked. See http://g.co/androidstudio/not-mocked for details.

这是我到目前为止:

改造界面:

@GET("webapi/user/{ID}/image")
Observable<ResponseBody> getVehicleImage(
        @Path("ID") @NonNull final String id,
        @Query("width") @NonNull final int width,
        @Query("height") @NonNull final int height,
        @Query("view") @NonNull final ImageView view
);
Run Code Online (Sandbox Code Playgroud)

getImage() 方法:

public Observable<Bitmap> getVehicleImage(@NonNull String id, @NonNull …
Run Code Online (Sandbox Code Playgroud)

java android-library retrofit mockwebserver okhttp3

4
推荐指数
2
解决办法
1707
查看次数

MockWebServer:llegalStateException:start()已被调用

我尝试使用MockWebServer进行测试。

我想用模拟的响应进行UI测试,以便可以测试有效\无效的UI更改,例如登录或在登录API中显示错误。

但是,每次我运行代码时,都会得到以下异常:

java.lang.IllegalStateException: start() already called
Run Code Online (Sandbox Code Playgroud)

码:

@RunWith(AndroidJUnit4.class)
public class UITestPlayground {

    String testUrl = "http://testurl.com/";
    MockWebServer server = new MockWebServer();

    @Rule
    public IntentsTestRule<LoginActivity> mIntentsRule = new IntentsTestRule<>(LoginActivity.class);

    @Before
    public void beforeHelper() throws IOException {
        TestHelper.removeUserAndTokenIfAny(mIntentsRule.getActivity());
        URLS.baseUrl = testUrl;
        server.url(URLS.baseUrl);
        //try to shutting down the server JUT IN CASE...
        server.shutdown();
        server.start();

    }

    @After
    public void afterHelper() throws IOException {
        server.shutdown();
    }


    @Test
    public void invalidLoginDueNotValidJSONResponse() {

        server.enqueue(new MockResponse().setBody("Something not valid JSON response"));

        String emailToBeTyped = "tester@tester.com"; …
Run Code Online (Sandbox Code Playgroud)

android-testing mockwebserver

4
推荐指数
1
解决办法
1094
查看次数

如何模拟特定的 URL 路径?

我在测试期间使用 okhttp 来模拟我的 http 响应。

//Create a mock server
mockWebServer.start(8080)
mockWebServer.enqueue(MockResponse().setBody("").setResponseCode(HttpURLConnection.HTTP_OK))
Run Code Online (Sandbox Code Playgroud)

然而,这对每条路径的响应都是OK。

如何模拟特定的 url 而不是全部?

okhttp mockwebserver

4
推荐指数
1
解决办法
3823
查看次数