安卓的gradle与单元测试 [英] Android: unit testing with gradle

查看:118
本文介绍了安卓的gradle与单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我可以运行在Android Studio中就好了下面的单元测试,但我好一会才建立持续集成,所以我需要gradle这个才能够运行。

 包com.smartsocialmedia.tests;进口android.test.InstrumentationTestCase;进口com.smartsocialmedia.utility.DbConnector;进口org.apache.http.message.BasicNameValuePair;
进口org.json.JSONObject;进口的java.util.ArrayList;公共类MainTest扩展InstrumentationTestCase
{
/ *
 *测试DbConnector
 *公众的JSONObject getJsonObject(ArrayList的< BasicNameValuePair>参数,可以布尔isGet)
 *通false作为第二个参数,如果GET是用来而非POST
 *在这种情况下,我们呼吁认证/登录API中
 * /
公共无效testGETJSONOBJECT()抛出异常{
    //创建BasicNameValuePair的ArrayList重新present的登录信息。
    ArrayList的< BasicNameValuePair> postParams =新的ArrayList< BasicNameValuePair>();
    postParams.add(新BasicNameValuePair(电子邮件,myemail));
    postParams.add(新BasicNameValuePair(密码,输入mypassword));
    DbConnector DB =新DbConnector(认证/登录);    的JSONObject的JSONObject = db.getJsonObject(postParams,FALSE);    //检查服务器的响应是一个JSONObject
    assertNotNull(JSONObject的);
    //检查,我们预计在响应参数
    诠释有效= jsonObject.getInt(有效);
    的assertEquals(有效,1);
}
}

Basically./gradlew不符合这个测试做什么......我需要知道我需要什么的gradle中的改变,以便为它工作。

  buildscript {
库{
    mavenCentral()
}
    依赖{
    类路径'com.android.tools.build:gradle:0.9.+
    }
}
应用插件:'机器人'库{
    mavenCentral()
}安卓{
    compileSdkVersion 19
    buildToolsVersion '19 .0.0    lintOptions {
        checkReleaseBuilds假
        //或者,如果你preFER,您可以继续检查错误在发布版本,
        //但即使继续构建时发现错误:
        abortOnError假
    }defaultConfig {
    14的minSdkVersion
    targetSdkVersion 19
    版本code 7
    的versionName1.0.5
    testPackageNamecom.smartsocialmedia.tests
}buildTypes {
    发布 {
        runProguard假
        proguardFiles getDefaultProguardFile('proguard的-android.txt'),'proguard的-rules.txt
        }
    }
}依赖{
    编译com.android.support:support-v4:18.0.0
    编译org.apache.httpcomponents:httpmime:4.1.2@jar
    编译com.github.chrisbanes.actionbarpulltorefresh:库:+'
    编译文件(库/通用图像装载-1.9.1-与-sources.jar')
}


解决方案

您需要有至少1.1.0-RC1(2015年2月2日)的Andr​​oid插件的gradle


  

单元测试支持。单元测试code是当地的JVM上运行,反对的android.jar的一个​​特殊版本,它与流行的嘲讽框架(例如的Mockito)。

兼容

http://tool​​s.android.com/tech-docs/new-建立系统

I have the following unit test that I can run just fine in android studio, but I am trying to get it set up for continuous integration so I will need gradle to be able to run it.

package com.smartsocialmedia.tests;

import android.test.InstrumentationTestCase;

import com.smartsocialmedia.utility.DbConnector;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainTest extends InstrumentationTestCase
{
/*
 * Testing DbConnector
 * public JSONObject getJsonObject(ArrayList<BasicNameValuePair> params, boolean isGet)
 * pass false as second parameter if GET is to be used rather than POST
 * in this case we are calling auth/login in the API
 */
public void testGETJSONOBJECT() throws Exception {
    //Create an ArrayList of BasicNameValuePair to represent the login information.
    ArrayList<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>();
    postParams.add(new BasicNameValuePair("eMail", "myemail"));
    postParams.add(new BasicNameValuePair("Password", "mypassword"));
    DbConnector db = new DbConnector("auth/login");

    JSONObject jsonObject = db.getJsonObject(postParams, false);

    //check that the server response is a jsonObject
    assertNotNull(jsonObject);
    //check that we have expected parameters in the response
    int valid = jsonObject.getInt("valid");
    assertEquals(valid, 1);
}
}

Basically./gradlew doesn't do anything with this test ... I need to know what I need to change in gradle in order for it to work.

buildscript {
repositories {
    mavenCentral()
}
    dependencies {
    classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.0'

    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 19
    versionCode 7
    versionName "1.0.5"
    testPackageName "com.smartsocialmedia.tests"
}

buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile 'org.apache.httpcomponents:httpmime:4.1.2@jar'
    compile 'com.github.chrisbanes.actionbarpulltorefresh:library:+'
    compile files('libs/universal-image-loader-1.9.1-with-sources.jar')
}

解决方案

You need to have at least 1.1.0-rc1 (2015/02/02) android gradle plugin

Unit testing support. Unit testing code is run on the local JVM, against a special version of android.jar that is compatible with popular mocking frameworks (e.g. Mockito).

http://tools.android.com/tech-docs/new-build-system

这篇关于安卓的gradle与单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆