Testing environment
Before you proceed the process, Java 1.8 should be installed first in your system.Here is my testing system:
OS: MacOSX Yosemite(Version 10.10.3) Android Studio: Version 1.2 Java SDK: Version 1.8.0_45
Project structure
/Project
/app
/src
/main
/java
AndroidManifest.xml
/testDebug
/java
build.gradle
/build
/gradle
build.gradle
Configuration /Project/build.gradle
Add dependencies forretrolambda and robolectric plugins under dependencies block.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.2'
classpath 'me.tatarka:gradle-retrolambda:3.1.0'
classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'
}
}
allprojects {
repositories {
jcenter()
}
}
Configuration /Project/app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'org.robolectric'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.spark.example.rxjava"
minSdkVersion 10
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'io.reactivex:rxandroid:0.24.0'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
}
robolectric {
// Configure includes / excludes
include '**/*Test.class'
exclude '**/espresso/**/*.class'
// Configure max heap size of the test JVM
maxHeapSize = '2048m'
// Configure the test JVM arguments - Does not apply to Java 8
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'
// Specify max number of processes (default is 1)
maxParallelForks = 4
// Specify max number of test classes to execute in a test process
// before restarting the process (default is unlimited)
forkEvery = 150
// configure whether failing tests should fail the build
ignoreFailures true
// use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for ${descriptor.name} with result: ${result.resultType}"
}
}
Simple unit test
package com.spark.example.rxjava;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.util.concurrent.CountDownLatch;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.functions.Action1;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
@Config(emulateSdk = 18, manifest = "app/src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class SimpleTest {
private static final String TEST_STR = "Hello, RxAndroid";
private String actual;
private CountDownLatch latch;
@Before
public void setUp() throws Exception {
latch = new CountDownLatch(1);
}
@Test
public void testHelloRxAndroid() throws Exception {
Observable helloObservable = Observable.create(subscriber -> handleObserver(subscriber));
helloObservable.subscribe(new HelloSubscriber());
latch.await();
assertThat(actual, is(TEST_STR));
}
@Test
public void testJustHello() throws InterruptedException {
Observable justObservable = Observable.just(TEST_STR);
justObservable.subscribe(new HelloSubscriber());
latch.await();
assertThat(actual, is(TEST_STR));
}
@Test
public void testWithAction() throws InterruptedException {
Observable observable = Observable.create(subscriber -> handleObserver(subscriber));
observable.subscribe(s -> {
actual = s;
latch.countDown();
});
latch.await();
assertThat(actual, is(TEST_STR));
}
private void handleObserver(Subscriber subscriber) {
subscriber.onNext(TEST_STR);
subscriber.onCompleted();
}
private class HelloSubscriber extends Subscriber {
@Override
public void onCompleted() {
latch.countDown();
}
@Override
public void onError(Throwable e) {
actual = "onError";
latch.countDown();
}
@Override
public void onNext(String s) {
actual = s;
}
}
}
I think Lambda is one of quite useful features Java 8 supports. You can easily compare both and guess the Lambda's benefit.
No Lambdaobservable.subscribe(new Action1Lambda() { @Override public void call(String s) { actual = s; latch.countDown(); } });
observable.subscribe(s -> {
actual = s;
latch.countDown();
});
Comments
Post a Comment