Presentation by Simon Vergauwen about Spek.
Spek is a BDD testing framework in Kotlin, written by Jetbrains. This talk also covers advanced testing with unMock, Mockito & PowerMock.
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")) } } })
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"))
Coding Example Simple Method java.lang.RuntimeException: Method beginObject in android.util.JsonWriter not mocked. See http://g.co/androidstudio/not-mocked for details. at android.util.JsonWriter.beginObject(JsonWriter.java) at be.appfoundry.spekdemo.util.JsonUtil.writeToJson(JsonWriter.kt:12) at be.appfoundry.spekdemo.util.JsonWriterSpekTest $1$1$1$1.invoke(JsonWriterSpekTest.kt:21) at be.appfoundry.spekdemo.util.JsonWriterSpekTest $1$1$1$1.invoke(JsonWriterSpekTest.kt:14) at org.jetbrains.spek.engine.Scope$Test.execute(Scope.kt:110) at org.jetbrains.spek.engine.Scope$Test.execute(Scope.kt:83)
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"))
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"))
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"))
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"))
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"))
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) }
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\"")) } } } })
Coding Example Iterative testing @RunWith(JUnitPlatform::class) class MainPresenterSpekTest : Spek({ describe("The MainPresenter is handling the MainView") { var mainPresenter = MainPresenter() var mainView = mock()
Coding Example Iterative testing @RunWith(JUnitPlatform::class) class MainPresenterSpekTest : Spek({ describe("The MainPresenter is handling the MainView") { var mainPresenter = MainPresenter() var mainView = mock()
Coding Example Iterative testing @RunWith(JUnitPlatform::class) class MainPresenterSpekTest : Spek({ describe("The MainPresenter is handling the MainView") { var mainPresenter = MainPresenter() var mainView = mock()
Coding Example Iterative testing @RunWith(JUnitPlatform::class) class MainPresenterSpekTest : Spek({ describe("The MainPresenter is handling the MainView") { var mainPresenter = MainPresenter() var mainView = mock()
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") })
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)) } } })
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
Coding Example JUnit rule for Rx public class RxJavaResetRule implements TestRule { @Override public Statement apply(final Statement base, final Description description) {
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
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);
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() }
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)
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)
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)