Examples for Spek article (#4106)

* Examples for BAEL-1169

* Examples for Spek
This commit is contained in:
Graham Cox 2018-05-11 13:35:04 +01:00 committed by Grzegorz Piwowarek
parent 537c1d1150
commit b677208899
5 changed files with 152 additions and 0 deletions

View File

@ -63,6 +63,24 @@
<artifactId>kotlinx-coroutines-core</artifactId>
<version>${kotlinx.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.spek</groupId>
<artifactId>spek-api</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.spek</groupId>
<artifactId>spek-subject-extension</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.spek</groupId>
<artifactId>spek-junit-platform-engine</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.nhaarman</groupId>
<artifactId>mockito-kotlin</artifactId>

View File

@ -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<Calculator>({
subject { Calculator() }
describe("A calculator") {
describe("Addition") {
val result = subject.add(3, 5)
it("Produces the correct answer") {
assertEquals(8, result)
}
}
}
})

View File

@ -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)
}
}
}
})

View File

@ -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())
}
}
}
}
})

View File

@ -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")
}
}
}
})