Leoš Přikryl
@leos_prikryl
commity.cz, GDG Jihlava
class MyTests : StringSpec({
"String.length should return size of string" {
"hello".length shouldBe 5
}
})
class MyTests : FunSpec({
test("String.length should return the length of the string") {
"sammy".length shouldBe 5
"".length shouldBe 0
}
})
class AnnotationSpecExample : AnnotationSpec() {
@BeforeEach
fun beforeTest() {
println("Before each test")
}
@Test
fun test1() {
1 shouldBe 1
}
@Test
fun test2() {
3 shouldBe 3
}
}
class MyTests : DescribeSpec({
describe("score") {
it("start as zero") {
// test here
}
context("with a strike") {
it("adds ten") {
// test here
}
it("carries strike to the next frame") {
// test here
}
}
}
}
obj.shouldBe(other)
expr.shouldBeTrue()
expr.shouldBeFalse()
shouldThrow<InvalidArgumentException> {
// a code that might throw an exception
}
val exception = shouldThrow<InvalidArgumentException> {
// a code that might throw an exception
}
exception.message.shouldStartWith("Something went wrong")
obj.shouldBeInstanceOf<String>()
service.javaClass.shouldHaveAnnotation(Service::class.java)
collection.shouldContainDuplicates()
list.shouldBeSorted()
uri.shouldHaveHost(host)
uri.shouldHavePath(path)
file.shouldBeLarger(otherFile)
file.shouldBeHidden()
date.shouldHaveSameDayAs(otherDate)
future.shouldBeCancelled()
thread.shouldBeAlive()
a.shouldBe(b)
a.shouldStartWith("foo")
a.shouldNotStartWith("foo")
a shouldBe b
a shouldStartWith "foo"
a should startWith("foo")
a shouldNotStartWith("foo")
a shouldNot startWith("foo")
assertSoftly {
text shouldHaveMinLength 5
text shouldStartWith "A"
text shouldEndWith "Z"
}
class StringSpecExample : StringSpec({
"Names should be long enough and shouldn't contain space" {
val names = listOf("John", "Fred", "James")
names.forAll {
it.shouldNotContain(" ")
it.shouldHaveMinLength(4)
}
}
})
forNone, forOne, forAny, forAtLeastOne, forAtMost(n), …
class TestWithListeners : StringSpec() {
override fun beforeTest(testCase: TestCase) {
//...
}
override fun afterTest(testCase: TestCase, result: TestResult) {
//...
}
override fun beforeSpec(spec: Spec) {
//...
}
override fun afterSpec(spec: Spec) {
//...
}
}
object MyListener : TestListener {
override fun beforeTest(testCase: TestCase) {
//...
}
override fun afterTest(testCase: TestCase, result: TestResult) {
//...
}
override fun beforeSpec(spec: Spec) {
//...
}
override fun afterSpec(spec: Spec) {
//...
}
}
class TestWithListeners : StringSpec() {
override fun listeners() = listOf(MyListener)
}
object ProjectConfig : AbstractProjectConfig() {
override fun parallelism(): Int = 2
}
class TestCaseConfigTest : FunSpec() {
init {
test("Test Case config showcase").config(
invocations = 6,
parallelism = 2,
timeout = 2.seconds,
enabled = true,
tags = setOf(Windows, MacOS)
) {
// ...
}
}
}
class TestIsolationTest : DescribeSpec() {
override fun isolationMode(): IsolationMode = IsolationMode.SingleInstance
init {
describe("Describe") {
println("Describe")
it("It 1") {
println("It 1")
}
it("It 2") {
println("It 2")
}
}
}
}
// Describe
// It 1
// It 2
class TestIsolationTest : DescribeSpec() {
override fun isolationMode(): IsolationMode = IsolationMode.InstancePerTest
init {
describe("Describe") {
println("Describe")
it("It 1") {
println("It 1")
}
it("It 2") {
println("It 2")
}
}
}
}
// Describe
// Describe
// It 1
// Describe
// It 2
class TestIsolationTest : DescribeSpec() {
override fun isolationMode(): IsolationMode = IsolationMode.InstancePerLeaf
init {
describe("Describe") {
println("Describe")
it("It 1") {
println("It 1")
}
it("It 2") {
println("It 2")
}
}
}
}
// Describe
// It 1
// Describe
// It 2
class EventualTest: StringSpec({
"should eventually succeed" {
eventually(5.seconds) {
// code that might fail at first,
// but should succeed within the given duration
}
}
})
class EventualTest: StringSpec({
"should continually succeed" {
continually(5.seconds) {
// code that should succeed
// and continue to succeed for the given duration
}
}
})
class TableDrivenTest: StringSpec({
"square roots" {
forall(
row(2, 4),
row(3, 9),
row(4, 16),
row(5, 25)
) { root, square ->
root * root shouldBe square
}
}
})
class StringConcatenationTest: StringSpec({
"String concatenation" {
assertAll { a: String, b: String ->
val concatenated = a + b
concatenated.length shouldBe a.length + b.length
concatenated shouldStartWith a
concatenated shouldEndWith b
}
}
})
object ProjectConfig : AbstractProjectConfig() {
override fun extensions(): List<ProjectLevelExtension> =
listOf(SpringAutowireConstructorExtension)
}
@SpringBootTest
class SpringInjectionTest(
val springBean: SpringBean
) : StringSpec({
"bean should be injected" {
springBean shouldNotBe null
}
})
"Check Environment variables" {
withEnvironment("env1" to "env value 1") {
System.getenv("env1") shouldBe "env value 1"
}
}
class ExitTest : StringSpec() {
override fun listeners() = listOf(SpecSystemExitListener)
init {
"Should exit with code 42" {
val thrown = shouldThrow<SystemExitException> {
System.exit(42)
}
thrown.exitCode shouldBe 42
}
}
}