@mSolo
2015-04-13T23:30:36.000000Z
字数 7867
阅读 1685
Jekins
Android
CI
持续集成
TDD
项目 build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.13.2'
classpath 'org.robolectric:robolectric-gradle-plugin:0.13.+'
}
}
allprojects {
repositories {
jcenter()
}
}
App build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 18
buildToolsVersion '19.1.0'
defaultConfig {
applicationId "com.msolo.stockeye"
minSdkVersion 16
targetSdkVersion 18
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'LICENSE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:19+'
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('junit:junit:4.+') {
exclude module: 'hamcrest-core'
}
androidTestCompile('org.robolectric:robolectric:2.3') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'support-v4'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
}
apply plugin: 'robolectric'
| build.gradle
| src
| |- androidTest
| |- java/com/msolo/stockeye/MainActivityTest.java
| |- java/com/msolo/stockeye/StockeyeApplication.java
| |- main
| |- java/com/msolo/stockeye/MainActivity.java
| |- java/com/msolo/stockeye/StockeyeApplication.java
@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {
@Test
public void testActivityDisplayHelloWorldSuccessful() {
String hello = new MainActivity().getResources().getString(R.string.hello_world);
assertEquals("Hello World!", hello);
}
}
更多例子
public void testListScrolling() {
listView.scrollTo(0, 0);
TouchUtils.dragQuarterScreenUp(this, activity);
int actualItemPosition = listView.getFirstVisiblePosition();
assertTrue("Wrong position", actualItemPosition > 0);
}
E:\android_workspaces\TDDStockeye>d:gradle-2.1\bin\gradle test
:app:preBuild
:app:preDebugBuild
:app:checkDebugManifest
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:compileTestDebugJava
:app:processTestDebugResources UP-TO-DATE
:app:testDebugClasses
:app:testDebug
:app:test
BUILD SUCCESSFUL
Total time: 11.647 secs
测试结果:file:///E:/android_workspaces/TDDStockeye/app/build/test-report/debug/index.html
注:TDD 快速测试(仅示范,真实项目中不必如此繁琐)
// NotificationStockQuoteTest.java
@Test
public void testSingletonOfNotificationStockQuote() {
notificationStockQuote = new NotificationStockQuote();
NotificationStockQuote notificationStockQuoteAnother = new NotificationStockQuote();
assertNotSame(notificationStockQuoteAnother, notificationStockQuote);
}
// NotificationStockQuote.java
public class NotificationStockQuote {
private static NotificationStockQuote sInstance = null;
private NotificationStockQuote() {}
public static NotificationStockQuote getInstance() {
return new NotificationStockQuote();
}
}
// NotificationStockQuoteTest.java
@Test
public void testSingletonOfNotificationStockQuote() {
NotificationStockQuote notificationStockQuoteAnother = NotificationStockQuote.getInstance();
notificationStockQuote = NotificationStockQuote.getInstance();
assertSame(notificationStockQuoteAnother, notificationStockQuote);
}
// NotificationStockQuote.java
public class NotificationStockQuote {
private static NotificationStockQuote sInstance = null;
private NotificationStockQuote() {}
public static NotificationStockQuote getInstance() {
return new NotificationStockQuote();
}
}
// NotificationStockQuoteTest.java
@Before
public void setUp() {
notificationStockQuote = NotificationStockQuote.getInstance();
}
@Test
public void testSingletonOfNotificationStockQuote() {
NotificationStockQuote notificationStockQuoteAnother = NotificationStockQuote.getInstance();
assertSame(notificationStockQuoteAnother, notificationStockQuote);
}
// NotificationStockQuote.java
public class NotificationStockQuote {
private static NotificationStockQuote sInstance = null;
private NotificationStockQuote() {}
public static NotificationStockQuote getInstance() {
if (null == sInstance) {
sInstance = new NotificationStockQuote();
}
return sInstance;
}
}
// NotificationStockQuoteTest.java
@Test
public void testInit() {
NotificationManager notificationManager = (NotificationManager) Robolectric.application.getSystemService(Context.NOTIFICATION_SERVICE);
notificationStockQuote.init();
NotificationStockQuote.HelperTestNotificationStockQuote helper = notificationStockQuote.new HelperTestNotificationStockQuote();
assertNotNull(helper.getNotificationManager());
assertSame(notificationManager, helper.getNotificationManager());
}
// NotificationStockQuote.java
public void init() {
}
/**
*
* This is an inner class for helping test NotificationStockQuote,
* and it should be delete in production.
*
* Date | Name | Change
*--------+--------------------------------------------------
* 141119 | mSolo | REQ #001: add method getNotificationManager().
*
*/
public class HelperTestNotificationStockQuote {
public HelperTestNotificationStockQuote() {}
public NotificationManager getNotificationManager() {
return notificationManager;
}
}
@Test
public void testInit() {
NotificationManager notificationManager = (NotificationManager) Robolectric.application.getSystemService(Context.NOTIFICATION_SERVICE);
//notificationStockQuote.init();
MimicNotificationStockQuote mimic = new MimicNotificationStockQuote();
mimic.init();
assertNotNull(mimic.notificationManager);
assertSame(notificationManager, mimic.notificationManager);
}
/**
*
* This is an inner class for mimic NotificationStockQuote.
*
* Date | Name | Change
*--------+--------------------------------------------------
* 141119 | mSolo | REQ #001: mimic method init().
*
*/
private class MimicNotificationStockQuote {
public NotificationManager notificationManager = null;
public MimicNotificationStockQuote() {}
public void init() {
notificationManager = (NotificationManager)Robolectric.application.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
}
}
// NotificationStockQuote.java
public void init() {
notificationManager = (NotificationManager) StockeyeApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
}