java Android单元测试样本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Android单元测试样本相关的知识,希望对你有一定的参考价值。

package jp.co.sample.presentation.navigation;

import android.net.Uri;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import jp.co.sample.BuildConfig;
import jp.co.sample.data.repositories.UserInfo;

public class WebViewUrlBuilder {

    private final String URL_PARAM_VALUE_CATEGORY_ID_TOW = "2";
    private final String URL_PARAM_VALUE_CATEGORY_ID_ONE = "1";
    private final String URL_PARAM_CATEGORY_ID = "categoryID";
    private final String URL_PARAM_USER_ID = "userID";

    public String buildLendingWebViewUrl(String userId, UserInfo userInfo) {

        String workID = userInfo.getStatus().isContracted() ?
                URL_PARAM_VALUE_CATEGORY_ID_TWO :
                URL_PARAM_VALUE_CATEGORY_ID_ONE;

        String memberPageUrl = BuildConfig.MEMBER_PAGE_URL;
        Uri.Builder builder = Uri.parse(memberPageUrl).buildUpon()
                .appendQueryParameter(URL_PARAM_CATEGORY_ID, workID)
                .appendQueryParameter(URL_PARAM_USERID, userID);

        String requestUrl = builder.build().toString();

        return BuildConfig.WEB_BASE_URL + "sign_in?return_to=" + getURLEncStr(requestUrl);
    }

    private String getURLEncStr(String str){
        if(str == null) return null;
        try {
            return URLEncoder.encode(str , "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
package jp.co.sample.presentation.navigation;

import org.junit.Before;
import org.junit.Test;
import jp.co.sample.BuildConfig;
import jp.co.sample.data.entities.user.Status;
import jp.co.sample.data.repositories.UserInfo;
import timber.log.Timber;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class WebViewUrlBuilderTest {

    private WebViewUrlBuilder mBuilder;

    @Before
    public void setUp() throws Exception {
        mBuilder = new WebViewUrlBuilder();
    }

    @Test
    public void buildLendingWebViewUrl() {
        Status status = mock(Status.class);
        UserInfo userInfo = new UserInfo();
        userInfo.setStatus(status);
        when(userInfo.getStatus().isContracted()).thenReturn(true);
        String url = mBuilder.buildLendingWebViewUrl("A11111111", userInfo);
        assertThat(url, is(BuildConfig.WEB_BASE_URL + "sign_in?return_to=" + BuildConfig.MEMBER_PAGE_URL + "?categoryID=2&userId=A11111111"));
    }
}

以上是关于java Android单元测试样本的主要内容,如果未能解决你的问题,请参考以下文章

02_android下单元测试

Android Studio 单元测试配置

Android测试:从零开始2——local单元测试

Android Studio中进行单元测试

Android:带有改造和单元测试的网络调用

Android studio 怎么使用单元测试(不需要device)