MockWebServer:llegalStateException:已经调用了start()

时间:2018-01-31 11:45:46

标签: android-testing mockwebserver

我尝试使用MockWebServer进行测试。

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

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

public async Task<User> CreateUser(GraphServiceClient graphClient)
{
    // This snippet gets the tenant domain from the Organization object to construct the user's email address.
    var organization = await graphClient.Organization.Request().GetAsync();
    var domain = organization.CurrentPage[0].VerifiedDomains.ElementAt(0).Name;

    // Add the user.
    var userEmail = "TestUser@" + domain;
    var mailNickname = userEmail.Split(new char[] { '@' }).FirstOrDefault();

    return await graphClient.Users.Request().AddAsync(new User
    {
        AccountEnabled = true,
        DisplayName = "Test User",
        MailNickname = mailNickname,
        PasswordProfile = new PasswordProfile
        {
            Password = "super_strong_password"
        },
        UserPrincipalName = userEmail
    });
}

代码:

java.lang.IllegalStateException: start() already called

我做错了什么? .start()只调用一次,我甚至.shutdown()只调用 案例......我不明白它怎么能不止一次打电话。

提前致谢。

1 个答案:

答案 0 :(得分:5)

github的原始示例中,我发现顺序已颠倒。

启动服务器,然后设置它的网址。

而不是设置网址然后启动服务器。

有趣。