我在使此单元测试正常工作时遇到了一些麻烦,我似乎无法弄清楚。如果可以的话,请帮助检查以下代码和错误,看看是否对您有意义。我知道这很容易,但似乎无法正常工作。

我有一个问题(无法将mError解析为变量)。我做错了什么,我在MockWebViewClient类中创建了变量。现在,当我注释掉使用mError的代码行时,测试仍然可以运行。

但是现在,在运行测试时,下面的代码行上出现“ junit.framework.AssertionFailedError”

assertTrue(!(mWeb.getProgress() < 100));


提前致谢。

WebviewTest.java

private static final String VALID_URL = "http://www.google.com";
private static final String INVALID_URL = "http://invalid.url.doesnotexist987.com";
private static final long TIMEOUT = 5000;
private Enlighten_Webview_Main mActivity;
private WebView mWeb;
private WebViewClient mMockWebViewClient;

// Rest of my code....

public final void testLoadValidUrl() {
        assertLoadUrl(VALID_URL);
        assertFalse(mMockWebViewClient, mError);
    }

    public final void testLoadInvalidUrl() {
        assertLoadUrl(INVALID_URL);
        assertTrue(mMockWebViewClient.mError);
    }

    private void assertLoadUrl(String url) {
        mWeb.loadUrl(url);
        sleep();

            //Added to hopefully let webview load all the way
        getInstrumentation().waitForIdleSync();

        assertTrue(!(mWeb.getProgress() < 100));
    }

    private void sleep() {
        try {
            Thread.sleep(TIMEOUT);
        } catch (InterruptedException e) {
            fail("Unexpected timeout");
        }
    }
    public class MockWebViewClient extends WebViewClient {
        boolean mError;

        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            mError = true;
        }
    }


编辑

我通过将我的mMockWebViewClient变量切换为正确的类型MockWebViewClient来解决此问题。由于MockWebViewClient类包含我在mError中需要的testLoadValidUrl布尔值

最佳答案

您的testLoadValidUrl方法中未定义变量“ mError”,可能是拼写错误。当您可能要用句号“”时,会有一个逗号“,”。供会员访问。

即更改

public final void testLoadValidUrl() {
    assertLoadUrl(VALID_URL);
    assertFalse(mMockWebViewClient, mError);
}




public final void testLoadValidUrl() {
    assertLoadUrl(VALID_URL);
    assertFalse(mMockWebViewClient.mError);
}

09-03 17:38