Skip to content

SCALA-25 - Intro to testing with ScalaTest #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ project/plugins/project/
# Scala-IDE specific
.scala_dependencies
.worksheet
.idea
.idea
/project/
8 changes: 8 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ lazy val scala_lang = (project in file("scala-lang"))
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
)

lazy val scala_test = (project in file("scala-test"))
.settings(
name := "scala-test",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
libraryDependencies += "org.scalamock" %% "scalamock" % "4.4.0" % Test,
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
)
10 changes: 10 additions & 0 deletions scala-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## ScalaTest Tutorials

This module contains articles about the ScalaTest framework in Scala.

### Relevant articles

- [Introduction to Testing With ScalaTest](https://www.baeldung.com/scala/scalatest)



Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.baeldung.scala.scalatest

import org.scalatest.{FlatSpec, Matchers}
import org.scalactic.StringNormalizations._

class ExampleFlatSpecWithMatchers extends FlatSpec with Matchers {

"With a matcher" should "let us check equality" in {
val number = 25
number should equal(25)
number shouldEqual 25
}

it should "also let us customize equality" in {
" baeldung " should equal("baeldung")(after being trimmed)
}

it should "let us check equality using be" in {
val number = 25
number should be(25)
number shouldBe 25
}

it should "let us check the length of strings" in {
val text = "baeldung"
text should have length 8
}

it should "let us check the size of collections" in {
val days = List("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
days should have size 7
}

it should "let us check different parts of a string" in {
val headline = "Baeldung is really cool!"
headline should startWith("Baeldung")
headline should endWith("cool!")
headline should include("really")
}

it should "let us check that an email is valid" in {
"[email protected]" should fullyMatch regex """[^@]+@[^\.]+\..+"""
}

ignore should "let us check a list is empty" in {
List.empty shouldBe empty
}

it should "let us check a list is NOT empty" in {
List(1, 2, 3) should not be empty
}

it should "let us check a map contains a given key and value" in {
Map('x' -> 10, 'y' -> 20, 'z' -> 30) should contain('y' -> 20)
}

it should "let us check the type of an object" in {
List(1, 2, 3) shouldBe a[List[_]]
List(1, 2, 3) should not be a[Map[_, _]]
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.scala.scalatest

import org.scalatest.FlatSpec

class ListFlatSpec extends FlatSpec {

"An empty List" should "have size 0" in {
assert(List.empty.size == 0)
}

it should "throw an IndexOutOfBoundsException when trying to access any element" in {
val emptyList = List()
assertThrows[IndexOutOfBoundsException] {
emptyList(1)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.scala.scalatest

import org.scalatest.FunSpec

class ListFunSpec extends FunSpec {

describe("A List") {
describe("when empty") {
it("should have size 0") {
assert(List.empty.size == 0)
}

it("should throw an IndexOutOfBoundsException when to access an element") {
val emptyList = List()
assertThrows[IndexOutOfBoundsException] {
emptyList(1)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.scala.scalatest

import org.scalatest.FunSuite

class ListFunSuite extends FunSuite {

test("An empty List should have size 0") {
assert(List.empty.size == 0)
}

test("Accessing invalid index should throw IndexOutOfBoundsException") {
val fruit = List("Banana", "Pineapple", "Apple")
assert(fruit.head == "Banana")
assertThrows[IndexOutOfBoundsException] {
fruit(5)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.scala.scalatest

import org.scalatest.{FlatSpec, Matchers}
import org.scalamock.scalatest.MockFactory


class ScalaMockFlatSpec extends FlatSpec with MockFactory with Matchers {

"A mocked Foo" should "return a mocked bar value" in {
val mockFoo = mock[Foo]
mockFoo.bar _ expects() returning 6

mockFoo.bar should be(6)
}
}

class Foo {
def bar = 100
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.scala.scalatest

import org.scalatest._
import collection.mutable.ListBuffer

class StringFlatSpecWithBeforeAndAfter extends FlatSpec with BeforeAndAfter {

val builder = new StringBuilder

before {
builder.append("Baeldung ")
}

after {
builder.clear()
}

"Baeldung" should "be interesting" in {
assert(builder.toString === "Baeldung ")

builder.append("is very interesting!")
assert(builder.toString === "Baeldung is very interesting!")
}

it should "have great tutorials" in {
assert(builder.toString === "Baeldung ")

builder.append("has great tutorials!")
assert(builder.toString === "Baeldung has great tutorials!")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.scala.scalatest

import org.scalatest.{FlatSpec, Matchers, Tag}

object BaeldungJavaTag extends Tag("com.baeldung.scala.scalatest.BaeldungJavaTag")

class TaggedFlatSpec extends FlatSpec with Matchers {

"Baeldung" should "be interesting" taggedAs (BaeldungJavaTag) in {
"Baeldung has articles about Java" should include("Java")
}

}