Slide 1

Slide 1 text

Cooking with Spek

Slide 2

Slide 2 text

Your host Simon Vergauwen Android developer [email protected] @vergauwen_simon

Slide 3

Slide 3 text

AppFoundry

Slide 4

Slide 4 text

A Specification Framework

Slide 5

Slide 5 text

Cooking with Spek Spek Clean Testing Basic spek test Spek structure Iterative testing Technology Compatibility Kits (TCK)

Slide 6

Slide 6 text

A Specification Framework

Slide 7

Slide 7 text

Basic Spek test

Slide 8

Slide 8 text

Coding Example Simple Method @file:JvmName("JsonUtil")
 package be.appfoundry.spekdemo.util
 
 import android.support.v4.util.ArrayMap
 import android.util.JsonWriter
 import java.io.StringWriter fun writeToJson(values: ArrayMap): String {
 // Impl
 }

Slide 9

Slide 9 text

Coding Example unMock apply plugin: 'de.mobilej.unmock'
 unMock {
 keep "android.util.JsonWriter"
 keep "android.util.JsonScope"
 } classpath “de.mobilej.unmock:UnMockPlugin:0.5.0” https://github.com/bjoernQ/unmock-plugin

Slide 10

Slide 10 text

Coding Example JUnit Test public class JsonUtilUnitTest {
 
 ArrayMap arrayMap;
 
 @Before
 public void setUp() throws Exception {
 arrayMap = new ArrayMap<>();
 } 
 @After
 public void tearDown() throws Exception { }
 }

Slide 11

Slide 11 text

Coding Example JUnit Test public class JsonUtilUnitTest {
 
 ArrayMap arrayMap;
 
 @Before
 public void setUp() throws Exception {
 arrayMap = new ArrayMap<>();
 } 
 @Test
 public void testWriteSingleMapToJSON() {
 arrayMap.put("A","B");
 String json = JsonUtil.writeToJson(arrayMap);
 assertThat(json, containsString("\"A\":\"B\""));
 }
 
 @After
 public void tearDown() throws Exception { }
 }

Slide 12

Slide 12 text

Coding Example JUnit Test

Slide 13

Slide 13 text

Coding Example JUnit Test

Slide 14

Slide 14 text

Coding Example Spek setup //setup kotlin testCompile "org.jetbrains.kotlin:kotlin-stdlib:1.0.3"
 testCompile “org.jetbrains.kotlin:kotlin-test:1.0.3” //setup spek
 testCompile "org.jetbrains.spek:spek-api:1.0.89"
 testCompile “org.jetbrains.spek:spek-junit-platform-engine:1.0.89" //setup unit platform runner
 testCompile "org.junit.platform:junit-platform-runner:1.0.0-M2"

Slide 15

Slide 15 text

Coding Example Basic Spek test class JsonWriterUnitTest : Spek({
 
 })

Slide 16

Slide 16 text

Coding Example Basic Spek test @RunWith(JUnitPlatform::class) class JsonWriterUnitTest : Spek({
 
 })

Slide 17

Slide 17 text

Coding Example Basic Spek test @RunWith(JUnitPlatform::class) class JsonWriterUnitTest : Spek({
 context("A json writer") {
 
 }
 })

Slide 18

Slide 18 text

Coding Example Basic Spek test @RunWith(JUnitPlatform::class) class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 
 }
 }
 })

Slide 19

Slide 19 text

Coding Example Basic Spek test @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 
 it("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 }
 }
 }
 })

Slide 20

Slide 20 text

Coding Example Basic Spek test

Slide 21

Slide 21 text

Spek structure

Slide 22

Slide 22 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 
 it("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 }
 }
 }
 })

Slide 23

Slide 23 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 
 it("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 }
 }
 }
 })

Slide 24

Slide 24 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 
 it("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 }
 }
 }
 }) abstract class Spek(val spec: Dsl.() -> Unit)

Slide 25

Slide 25 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 
 it("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 }
 }
 }
 })

Slide 26

Slide 26 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") { beforeEach {//context beforeEach }
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 
 it("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 }
 } afterEach {//context afterEach }
 }
 })

Slide 27

Slide 27 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 
 it("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 }
 }
 }
 })

Slide 28

Slide 28 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 beforeEach { //given beforeEach } 
 it("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 } afterEach { //given beforeEach }
 }
 }
 })

Slide 29

Slide 29 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") { beforeEach {//context beforeEach } 
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B"))
 beforeEach { //given beforeEach } 
 it("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 } afterEach { //given beforeEach }
 } afterEach {//context afterEach }
 }
 })

Slide 30

Slide 30 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B")) 
 then("should contain A:B") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 }
 }
 }
 }) fun Dsl.then(description: String, body: () -> Unit) {
 test("then $description", body = body)
 }

Slide 31

Slide 31 text

Coding Example Spek structure @RunWith(JUnitPlatform::class)
 class JsonWriterUnitTest : Spek({
 context("A json writer") {
 given("a single value map") {
 val singleValueMap = arrayMapOf(Pair("A", "B")) 
 xit("should contain A:B",reason = "pending version 2") {
 val json = writeToJson(singleValueMap)
 assertThat(json, containsString("\"A\":\"B\""))
 } }
 }
 })

Slide 32

Slide 32 text

Iterative testing

Slide 33

Slide 33 text

Coding Example JUnit example @RunWith(Parameterized.class)
 public class MainPresenterUnitTest {
 
 @Parameters
 public static String[] data() {
 return new String[]{"[email protected]", "[email protected]",
 “[email protected]", "@AppFoundryBE"};
 }
 
 public MainPresenterUnitTest(String input) {
 this.input = input;
 }
 
 @Before
 public void setUp() {
 mainView = mock(MainView.class);
 mainPresenter = new MainPresenter();
 mainPresenter.attachView(mainView);
 }
 
 @Test
 public void testIfItemIsShowAsEmail() {
 mainPresenter.processText(input);
 verify(mainView, times(1)).showItem(argThat(isA(MailItem.class)));
 }
 }

Slide 34

Slide 34 text

Coding Example JUnit example @RunWith(Parameterized.class)
 public class MainPresenterUnitTest {
 
 @Parameters
 public static String[] data() {
 return new String[]{"[email protected]", "[email protected]",
 “[email protected]", "@AppFoundryBE"};
 }
 
 public MainPresenterUnitTest(String input) {
 this.input = input;
 }
 
 @Before
 public void setUp() {
 mainView = mock(MainView.class);
 mainPresenter = new MainPresenter();
 mainPresenter.attachView(mainView);
 }
 
 @Test
 public void testIfItemIsShowAsEmail() {
 mainPresenter.processText(input);
 verify(mainView, times(1)).showItem(argThat(isA(MailItem.class)));
 }
 }

Slide 35

Slide 35 text

Coding Example JUnit example @RunWith(Parameterized.class)
 public class MainPresenterUnitTest {
 
 @Parameters
 public static String[] data() {
 return new String[]{"[email protected]", "[email protected]",
 “[email protected]", "@AppFoundryBE"};
 }
 
 public MainPresenterUnitTest(String input) {
 this.input = input;
 }
 
 @Before
 public void setUp() {
 mainView = mock(MainView.class);
 mainPresenter = new MainPresenter();
 mainPresenter.attachView(mainView);
 }
 
 @Test
 public void testIfItemIsShowAsEmail() {
 mainPresenter.processText(input);
 verify(mainView, times(1)).showItem(argThat(isA(MailItem.class)));
 }
 }

Slide 36

Slide 36 text

Coding Example JUnit example @RunWith(Parameterized.class)
 public class MainPresenterUnitTest {
 
 @Parameters
 public static String[] data() {
 return new String[]{"[email protected]", "[email protected]",
 “[email protected]", "@AppFoundryBE"};
 }
 
 public MainPresenterUnitTest(String input) {
 this.input = input;
 }
 
 @Before
 public void setUp() {
 mainView = mock(MainView.class);
 mainPresenter = new MainPresenter();
 mainPresenter.attachView(mainView);
 }
 
 @Test
 public void testIfItemIsShowAsEmail() {
 mainPresenter.processText(input);
 verify(mainView, times(1)).showItem(argThat(isA(MailItem.class)));
 }
 }

Slide 37

Slide 37 text

Coding Example JUnit example

Slide 38

Slide 38 text

Coding Example Iterative testing @RunWith(JUnitPlatform::class)
 class MainPresenterSpekTest : Spek({
 describe("The MainPresenter is handling the MainView") {
 var mainPresenter = MainPresenter()
 var mainView = mock()
 
 beforeEach {
 mainPresenter = MainPresenter()
 mainView = mock()
 mainPresenter.attachView(mainView)
 }
 
 on("processing a correctly formatted email adres") {
 listOf("[email protected]", "[email protected]",
 "[email protected]", "@AppFoundryBE").forEach { email ->
 it("should show $email") {
 mainPresenter.processText(email)
 verify(mainView, times(1)).showItem(argThat(isA(MailItem::class.java)))
 }
 }
 }
 }
 })

Slide 39

Slide 39 text

Coding Example Iterative testing @RunWith(JUnitPlatform::class)
 class MainPresenterSpekTest : Spek({
 describe("The MainPresenter is handling the MainView") {
 var mainPresenter = MainPresenter()
 var mainView = mock()
 
 beforeEach {
 mainPresenter = MainPresenter()
 mainView = mock()
 mainPresenter.attachView(mainView)
 }
 
 on("processing a correctly formatted email adres") {
 listOf("[email protected]", "[email protected]",
 "[email protected]", "@AppFoundryBE").forEach { email ->
 it("should show $email") {
 mainPresenter.processText(email)
 verify(mainView, times(1)).showItem(argThat(isA(MailItem::class.java)))
 }
 }
 }
 }
 })

Slide 40

Slide 40 text

Coding Example Iterative testing @RunWith(JUnitPlatform::class)
 class MainPresenterSpekTest : Spek({
 describe("The MainPresenter is handling the MainView") {
 var mainPresenter = MainPresenter()
 var mainView = mock()
 
 beforeEach {
 mainPresenter = MainPresenter()
 mainView = mock()
 mainPresenter.attachView(mainView)
 }
 
 on("processing a correctly formatted email adres") {
 listOf("[email protected]", "[email protected]",
 "[email protected]", "@AppFoundryBE").forEach { email -> 
 it("should show $email") {
 mainPresenter.processText(email)
 verify(mainView, times(1)).showItem(argThat(isA(MailItem::class.java)))
 }
 }
 }
 }
 })

Slide 41

Slide 41 text

Coding Example Iterative testing @RunWith(JUnitPlatform::class)
 class MainPresenterSpekTest : Spek({
 describe("The MainPresenter is handling the MainView") {
 var mainPresenter = MainPresenter()
 var mainView = mock()
 
 beforeEach {
 mainPresenter = MainPresenter()
 mainView = mock()
 mainPresenter.attachView(mainView)
 }
 
 on("processing a correctly formatted email adres") {
 listOf("[email protected]", "[email protected]",
 "[email protected]", "@AppFoundryBE").forEach { email -> 
 it("should show $email") {
 mainPresenter.processText(email)
 verify(mainView, times(1)).showItem(argThat(isA(MailItem::class.java)))
 }
 }
 }
 }
 })

Slide 42

Slide 42 text

Coding Example Iterative testing @RunWith(JUnitPlatform::class)
 class MainPresenterSpekTest : Spek({
 describe("The MainPresenter is handling the MainView") {
 var mainPresenter = MainPresenter()
 var mainView = mock()
 
 beforeEach {
 mainPresenter = MainPresenter()
 mainView = mock()
 mainPresenter.attachView(mainView)
 }
 
 on("processing a correctly formatted email adres") {
 listOf("[email protected]", "[email protected]",
 "[email protected]", "@AppFoundryBE").forEach { email -> 
 it("should show $email") {
 mainPresenter.processText(email)
 verify(mainView, times(1)).showItem(argThat(isA(MailItem::class.java)))
 }
 }
 }
 }
 })

Slide 43

Slide 43 text

Coding Example Iterative testing

Slide 44

Slide 44 text

Technology Compatibility Kits (TCK)

Slide 45

Slide 45 text

TCK Item MailItem PhoneItem TwitterItem URLItem

Slide 46

Slide 46 text

Coding Example TCK abstract class ItemTCK(val item: Item) : Spek({
 //spek test
 })
 
 class URLItemTCKTest : ItemTCK(URLItem("www.appfoundry.be"))
 
 class TwitterItemTCKTest : ItemTCK(TwitterItem("@AppFoundryBE"))
 
 class MailItemTCKTest : ItemTCK(MailItem("[email protected]"))
 
 class PhoneItemTCKTest : ItemTCK(PhoneItem("003238719966"))

Slide 47

Slide 47 text

Coding Example TCK abstract class ItemTCK(val factory: () -> Item) : Spek({
 val item = factory() //spek test
 }) class URLItemTCKTest : ItemTCK({ URLItem("www.appfoundry.be") })
 
 class TwitterItemTCKTest : ItemTCK({ TwitterItem("@AppFoundryBE") })
 
 class MailItemTCKTest : ItemTCK({ MailItem("[email protected]") })
 
 class PhoneItemTCKTest : ItemTCK({ PhoneItem("003238719966") })

Slide 48

Slide 48 text

Coding Example TCK @RunWith(PowerMockRunner::class)
 @PowerMockRunnerDelegate(JUnitPlatform::class)
 @PrepareForTest(ContextCompat::class)
 abstract class ItemTCK(val factory: () -> Item) : Spek({
 val item = factory() 
 describe("handling an item object") {
 val context = mock()
 val drawable = mock()
 beforeEach {
 mockStatic(ContextCompat::class.java)
 whenever(ContextCompat.getDrawable(any(), any()))
 .thenReturn(drawable)
 }
 
 it("should never have a null name") {
 assertThat(item.name, notNullValue())
 }
 it("should always have a color id") {
 assertThat(item.itemColorId, isA(Int::class.java))
 }
 it("should always return a drawable") {
 assertThat(item.getIcon(context), equalTo(drawable))
 }
 }
 })

Slide 49

Slide 49 text

Coding Example TCK @RunWith(PowerMockRunner::class)
 @PowerMockRunnerDelegate(JUnitPlatform::class)
 @PrepareForTest(ContextCompat::class)
 abstract class ItemTCK(val factory: () -> Item) : Spek({
 val item = factory() 
 describe("handling an item object") {
 val context = mock()
 val drawable = mock()
 beforeEach {
 PowerMock.mockStatic(ContextCompat::class.java)
 whenever(ContextCompat.getDrawable(any(), any()))
 .thenReturn(drawable)
 }
 
 it("should never have a null name") {
 assertThat(item.name, notNullValue())
 }
 it("should always have a color id") {
 assertThat(item.itemColorId, isA(Int::class.java))
 }
 it("should always return a drawable") {
 assertThat(item.getIcon(context), equalTo(drawable))
 }
 }
 })

Slide 50

Slide 50 text

Coding Example TCK @RunWith(PowerMockRunner::class)
 @PowerMockRunnerDelegate(JUnitPlatform::class)
 @PrepareForTest(ContextCompat::class)
 abstract class ItemTCK(val factory: () -> Item) : Spek({
 val item = factory() 
 describe("handling an item object") {
 val context = mock()
 val drawable = mock()
 beforeEach {
 mockStatic(ContextCompat::class.java)
 whenever(ContextCompat.getDrawable(any(), any()))
 .thenReturn(drawable)
 }
 
 it("should never have a null name") {
 assertThat(item.name, notNullValue())
 }
 it("should always have a color id") {
 assertThat(item.itemColorId, isA(Int::class.java))
 }
 it("should always return a drawable") {
 assertThat(item.getIcon(context), equalTo(drawable))
 }
 }
 })

Slide 51

Slide 51 text

Simplify your tests

Slide 52

Slide 52 text

Coding Example Simple Rx function fun doSomeRxing(): Observable =
 Observable.just(1L).observeOn(AndroidSchedulers.mainThread())

Slide 53

Slide 53 text

Coding Example Simple Rx function java.lang.ExceptionInInitializerError at rx.android.schedulers.AndroidSchedulers.mainThread(AndroidSchedulers.java:39) at be.appfoundry.spekdemo.util.RxMethodKt.doSomeRxing(RxMethod.kt:8) at be.appfoundry.spekdemo.util.RxTest$1$1$2.invoke(RxTest.kt:22) at be.appfoundry.spekdemo.util.RxTest$1$1$2.invoke(RxTest.kt:16) at org.jetbrains.spek.engine.Scope$Test.execute(Scope.kt:110) at org.jetbrains.spek.engine.Scope$Test.execute(Scope.kt:83) ...
 
 Caused by: java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details. at android.os.Looper.getMainLooper(Looper.java) at rx.android.schedulers.AndroidSchedulers $MainThreadSchedulerHolder.(AndroidSchedulers.java:31) ... 32 more

Slide 54

Slide 54 text

Coding Example JUnit rule for Rx public class RxJavaResetRule implements TestRule {
 @Override
 public Statement apply(final Statement base, final Description description) {
 
 } }

Slide 55

Slide 55 text

Coding Example JUnit rule for Rx public class RxJavaResetRule implements TestRule {
 @Override
 public Statement apply(final Statement base, final Description description) {
 return new Statement() {
 @Override
 public void evaluate() throws Throwable {
 //@Before
 
 base.evaluate();
 
 //@After
 } 
 };
 } }

Slide 56

Slide 56 text

Coding Example JUnit rule for Rx public class RxJavaResetRule implements TestRule {
 @Override
 public Statement apply(final Statement base, final Description description) {
 return new Statement() {
 @Override
 public void evaluate() throws Throwable {
 //@Before
 RxJavaPlugins.getInstance().reset();
 RxJavaPlugins.getInstance().registerSchedulersHook(rxJavaHook);
 
 RxAndroidPlugins.getInstance().reset();
 RxAndroidPlugins.getInstance().registerSchedulersHook(rxAndroidHook);
 
 base.evaluate();
 
 //@After
 RxJavaPlugins.getInstance().reset();
 RxAndroidPlugins.getInstance().reset();
 } 
 };
 } //rxJavaHook & rxAndroidHook def }

Slide 57

Slide 57 text

Coding Example JUnit rule for Rx RxJavaSchedulersHook rxJavaSchedulersHook = new RxJavaSchedulersHook(){
 @Override
 public Scheduler getComputationScheduler() {
 return Schedulers.immediate();
 }
 
 @Override
 public Scheduler getNewThreadScheduler() {
 return Schedulers.immediate();
 }
 
 @Override
 public Scheduler getIOScheduler() {
 return Schedulers.immediate();
 }
 };
 
 RxAndroidSchedulersHook rxAndroidSchedulersHook = new RxAndroidSchedulersHook(){
 @Override
 public Scheduler getMainThreadScheduler() {
 return Schedulers.immediate();
 }
 };

Slide 58

Slide 58 text

Coding Example JUnit rule for Rx public class RxUnitTest {
 @Rule
 public RxJavaResetRule rxJavaResetRule = new RxJavaResetRule();
 
 TestSubscriber testSubscriber;
 
 @Before
 public void setUp() {
 testSubscriber = new TestSubscriber<>();
 }
 
 @Test
 public void testRx() {
 RxMethodKt.doSomeRxing().subscribe(testSubscriber);
 
 assertThat(testSubscriber,
 allOf(onNextEvents(hasSize(1)), onNextEvents(hasItem(1L)),
 hasNoErrors(),
 isCompleted()));
 }
 }

Slide 59

Slide 59 text

Coding Example JUnit rule for Rx public class RxUnitTest {
 @Rule
 public RxJavaResetRule rxJavaResetRule = new RxJavaResetRule();
 
 TestSubscriber testSubscriber;
 
 @Before
 public void setUp() {
 testSubscriber = new TestSubscriber<>();
 }
 
 @Test
 public void testRx() {
 RxMethodKt.doSomeRxing().subscribe(testSubscriber);
 
 assertThat(testSubscriber,
 allOf(onNextEvents(hasSize(1)), onNextEvents(hasItem(1L)),
 hasNoErrors(),
 isCompleted()));
 }
 }

Slide 60

Slide 60 text

Coding Example JUnit rule for Rx public class RxUnitTest {
 @Rule
 public RxJavaResetRule rxJavaResetRule = new RxJavaResetRule();
 
 TestSubscriber testSubscriber;
 
 @Before
 public void setUp() {
 testSubscriber = new TestSubscriber<>();
 }
 
 @Test
 public void testRx() {
 RxMethodKt.doSomeRxing().subscribe(testSubscriber);
 
 assertThat(testSubscriber,
 allOf(onNextEvents(hasSize(1)), onNextEvents(hasItem(1L)),
 hasNoErrors(),
 isCompleted()));
 }
 }

Slide 61

Slide 61 text

Coding Example Junit rule in Spek @RunWith(JUnitPlatform::class)
 class RxTest : Spek({
 on("testing something observed by an android schedulers") {
 var testSubscriber = TestSubscriber()
 beforeEach { testSubscriber = TestSubscriber() } 
 }
 })

Slide 62

Slide 62 text

Coding Example Junit rule in Spek @RunWith(JUnitPlatform::class)
 class RxTest : Spek({
 on("testing something observed by an android schedulers") {
 var testSubscriber = TestSubscriber()
 beforeEach { testSubscriber = TestSubscriber() }
 
 it("should be easily tested in unit test") {
 doSomeRxing().subscribe(testSubscriber) 
 assertThat(testSubscriber,
 allOf(hasNoErrors(),
 onNextEvents(hasSize(1)),
 onNextEvents(hasItem(1L)),
 isCompleted()))
 }
 }
 })

Slide 63

Slide 63 text

Coding Example Simple Rx function inline fun Dsl.rxGroup(description: String, pending: Pending = Pending.No,
 crossinline body: Dsl.() -> Unit) {
 
 group(description, pending) {
 }
 }

Slide 64

Slide 64 text

Coding Example Simple Rx function inline fun Dsl.rxGroup(description: String, pending: Pending = Pending.No,
 crossinline body: Dsl.() -> Unit) {
 
 group(description, pending) {
 beforeEach {
 RxJavaPlugins.getInstance().reset()
 RxJavaPlugins.getInstance().registerSchedulersHook(rxJavaHook)
 
 RxAndroidPlugins.getInstance().reset()
 RxAndroidPlugins.getInstance().registerSchedulersHook(rxAndroidHook)
 }
 
 body()
 
 afterEach {
 RxJavaPlugins.getInstance().reset()
 
 RxAndroidPlugins.getInstance().reset()
 }
 }
 }

Slide 65

Slide 65 text

Coding Example Simple Rx function inline fun Dsl.rxGroup(description: String, pending: Pending = Pending.No,
 crossinline body: Dsl.() -> Unit) {
 
 group(description, pending) {
 beforeEach {
 RxJavaPlugins.getInstance().reset()
 RxJavaPlugins.getInstance().registerSchedulersHook(rxJavaHook)
 
 RxAndroidPlugins.getInstance().reset()
 RxAndroidPlugins.getInstance().registerSchedulersHook(rxAndroidHook)
 }
 
 body()
 
 afterEach {
 RxJavaPlugins.getInstance().reset()
 
 RxAndroidPlugins.getInstance().reset()
 }
 }
 } inline fun Dsl.onRx(description: String, crossinline body: Dsl.() -> Unit) =
 rxGroup("on $description", body = body)

Slide 66

Slide 66 text

Coding Example Simple Rx function @RunWith(JUnitPlatform::class)
 class RxTest : Spek({
 on("when testing something observed by an android schedulers") {
 var testSubscriber = TestSubscriber()
 beforeEach { testSubscriber = TestSubscriber() }
 
 it("should be easily tested in unit test") {
 doSomeRxing().subscribe(testSubscriber) 
 assertThat(testSubscriber,
 allOf(hasNoErrors(),
 onNextEvents(hasSize(1)),
 onNextEvents(hasItem(1L)),
 isCompleted()))
 }
 }
 })

Slide 67

Slide 67 text

Coding Example Simple Rx function @RunWith(JUnitPlatform::class)
 class RxTest : Spek({
 onRx("when testing something observed by an android schedulers") {
 var testSubscriber = TestSubscriber()
 beforeEach { testSubscriber = TestSubscriber() }
 
 it("should be easily tested in unit test") {
 doSomeRxing().subscribe(testSubscriber) 
 assertThat(testSubscriber,
 allOf(hasNoErrors(),
 onNextEvents(hasSize(1)),
 onNextEvents(hasItem(1L)),
 isCompleted()))
 }
 }
 })

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

Questions? Simon Vergauwen Android developer [email protected] @vergauwen_simon

Slide 70

Slide 70 text

Thank you!