步驟
1.在Eclipse的菜單中依次點擊File-new-Project
2.然後選擇Android Test Project
3.填入項目名字,選擇為哪一個項目創建單元測試,然後就OK了
4.編寫測試用例
新建的類繼承與AndroidTestCase 就可以編寫測試類了
例:www.linuxidc.com
- public class StatusTest extends AndroidTestCase
- {
- public static final String TAG = "StatusTest";
- /**
- * 登陸授權測試
- * @throws Exception
- */
- public void testLogin() throws Exception
- {
- System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
- System.setProperty("weibo4j.oauth.consumerSecret",Weibo.CONSUMER_SECRET);
-
- String oauth_pin = null ;
- boolean loginResult = false ;
- //0.實例化Weibo
- Weibo weibo = OAuthConstant.getInstance().getWeibo();
- //1.從新浪獲取RequestToken。
- RequestToken requestToken = weibo.getOAuthRequestToken();
- // 2.通過授權地址獲得oauth_verifier授權碼www.linuxidc.com
- oauth_pin = OAuthConstant.getInstance().getWeibo().getOAuthPin("[email protected]", "xxxxxx",
- requestToken.getToken());
- // 3.RequestToken通過返回的授權碼獲取AccessToken
- AccessToken accessToken = requestToken.getAccessToken(oauth_pin);
- // 4.給微博設置accessToken和accesssSecret
- weibo.setToken(accessToken.getToken(), accessToken.getTokenSecret());
- // 5.驗證用戶合法性
- weibo.verifyCredentials();
- // 6.登錄成功
- loginResult = true;
- // 7.保存AccessToken等相關參數
- OAuthConstant.getInstance().setAccessToken(accessToken);
- OAuthConstant.getInstance().setRequestToken(requestToken);
- Log.d(TAG, "accessToken="+accessToken.getToken()+",TokenSecret="+accessToken.getTokenSecret());
- }
- }