From b677208899684e9326b23a8eab940dbb4ebb04b8 Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Fri, 11 May 2018 13:35:04 +0100 Subject: [PATCH] Examples for Spek article (#4106) * Examples for BAEL-1169 * Examples for Spek --- core-kotlin/pom.xml | 18 ++++++ .../kotlin/spek/CalculatorSubjectTest5.kt | 19 ++++++ .../baeldung/kotlin/spek/CalculatorTest5.kt | 32 ++++++++++ .../baeldung/kotlin/spek/DataDrivenTest5.kt | 21 +++++++ .../com/baeldung/kotlin/spek/GroupTest5.kt | 62 +++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 00c3ac188d..24bfb302cf 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -63,6 +63,24 @@ kotlinx-coroutines-core ${kotlinx.version} + + org.jetbrains.spek + spek-api + 1.1.5 + test + + + org.jetbrains.spek + spek-subject-extension + 1.1.5 + test + + + org.jetbrains.spek + spek-junit-platform-engine + 1.1.5 + test + com.nhaarman mockito-kotlin diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt new file mode 100644 index 0000000000..f595d65bf2 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorSubjectTest5.kt @@ -0,0 +1,19 @@ +package com.baeldung.kotlin.spek + +import com.baeldung.kotlin.junit5.Calculator +import org.jetbrains.spek.api.dsl.describe +import org.jetbrains.spek.api.dsl.it +import org.jetbrains.spek.subject.SubjectSpek +import org.junit.jupiter.api.Assertions.assertEquals + +class CalculatorSubjectTest5 : SubjectSpek({ + subject { Calculator() } + describe("A calculator") { + describe("Addition") { + val result = subject.add(3, 5) + it("Produces the correct answer") { + assertEquals(8, result) + } + } + } +}) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt new file mode 100644 index 0000000000..3c49d916e4 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/CalculatorTest5.kt @@ -0,0 +1,32 @@ +package com.baeldung.kotlin.spek + +import com.baeldung.kotlin.junit5.Calculator +import org.jetbrains.spek.api.Spek +import org.jetbrains.spek.api.dsl.describe +import org.jetbrains.spek.api.dsl.given +import org.jetbrains.spek.api.dsl.it +import org.jetbrains.spek.api.dsl.on +import org.junit.jupiter.api.Assertions.assertEquals + +class CalculatorTest5 : Spek({ + given("A calculator") { + val calculator = Calculator() + on("Adding 3 and 5") { + val result = calculator.add(3, 5) + it("Produces 8") { + assertEquals(8, result) + } + } + } + + describe("A calculator") { + val calculator = Calculator() + describe("Addition") { + val result = calculator.add(3, 5) + it("Produces the correct answer") { + assertEquals(8, result) + } + } + } + +}) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt new file mode 100644 index 0000000000..bbcb36e8bb --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/DataDrivenTest5.kt @@ -0,0 +1,21 @@ +package com.baeldung.kotlin.spek + +import org.jetbrains.spek.api.Spek +import org.jetbrains.spek.api.dsl.describe +import org.jetbrains.spek.api.dsl.it +import org.junit.jupiter.api.Assertions + +class DataDrivenTest5 : Spek({ + describe("A data driven test") { + mapOf( + "hello" to "HELLO", + "world" to "WORLD" + ).forEach { input, expected -> + describe("Capitalising $input") { + it("Correctly returns $expected") { + Assertions.assertEquals(expected, input.toUpperCase()) + } + } + } + } +}) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt new file mode 100644 index 0000000000..5aeee622e4 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/spek/GroupTest5.kt @@ -0,0 +1,62 @@ +package com.baeldung.kotlin.spek + +import org.jetbrains.spek.api.Spek +import org.jetbrains.spek.api.dsl.describe +import org.jetbrains.spek.api.dsl.it + +class GroupTest5 : Spek({ + describe("Outer group") { + beforeEachTest { + System.out.println("BeforeEachTest 0") + } + beforeGroup { + System.out.println("BeforeGroup 0") + } + afterEachTest { + System.out.println("AfterEachTest 0") + } + afterGroup { + System.out.println("AfterGroup 0") + } + describe("Inner group 1") { + beforeEachTest { + System.out.println("BeforeEachTest 1") + } + beforeGroup { + System.out.println("BeforeGroup 1") + } + afterEachTest { + System.out.println("AfterEachTest 1") + } + afterGroup { + System.out.println("AfterGroup 1") + } + it("Test 1") { + System.out.println("Test 1") + } + it("Test 2") { + System.out.println("Test 2") + } + } + describe("Inner group 2") { + beforeEachTest { + System.out.println("BeforeEachTest 2") + } + beforeGroup { + System.out.println("BeforeGroup 2") + } + afterEachTest { + System.out.println("AfterEachTest 2") + } + afterGroup { + System.out.println("AfterGroup 2") + } + it("Test 3") { + System.out.println("Test 3") + } + it("Test 4") { + System.out.println("Test 4") + } + } + } +})