Skip to content

Commit d1ee07e

Browse files
authored
Merge pull request #96 from JonCook/master
SCALA-25 - Intro to testing with ScalaTest
2 parents 7bc1600 + ab3e975 commit d1ee07e

File tree

10 files changed

+202
-1
lines changed

10 files changed

+202
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ project/plugins/project/
1515
# Scala-IDE specific
1616
.scala_dependencies
1717
.worksheet
18-
.idea
18+
.idea
19+
/project/

build.sbt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ lazy val scala_lang = (project in file("scala-lang"))
2323
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
2424
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
2525
)
26+
27+
lazy val scala_test = (project in file("scala-test"))
28+
.settings(
29+
name := "scala-test",
30+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
31+
libraryDependencies += "org.scalamock" %% "scalamock" % "4.4.0" % Test,
32+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
33+
)

scala-test/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## ScalaTest Tutorials
2+
3+
This module contains articles about the ScalaTest framework in Scala.
4+
5+
### Relevant articles
6+
7+
- [Introduction to Testing With ScalaTest](https://www.baeldung.com/scala/scalatest)
8+
9+
10+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.scala.scalatest
2+
3+
import org.scalatest.{FlatSpec, Matchers}
4+
import org.scalactic.StringNormalizations._
5+
6+
class ExampleFlatSpecWithMatchers extends FlatSpec with Matchers {
7+
8+
"With a matcher" should "let us check equality" in {
9+
val number = 25
10+
number should equal(25)
11+
number shouldEqual 25
12+
}
13+
14+
it should "also let us customize equality" in {
15+
" baeldung " should equal("baeldung")(after being trimmed)
16+
}
17+
18+
it should "let us check equality using be" in {
19+
val number = 25
20+
number should be(25)
21+
number shouldBe 25
22+
}
23+
24+
it should "let us check the length of strings" in {
25+
val text = "baeldung"
26+
text should have length 8
27+
}
28+
29+
it should "let us check the size of collections" in {
30+
val days = List("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
31+
days should have size 7
32+
}
33+
34+
it should "let us check different parts of a string" in {
35+
val headline = "Baeldung is really cool!"
36+
headline should startWith("Baeldung")
37+
headline should endWith("cool!")
38+
headline should include("really")
39+
}
40+
41+
it should "let us check that an email is valid" in {
42+
"[email protected]" should fullyMatch regex """[^@]+@[^\.]+\..+"""
43+
}
44+
45+
ignore should "let us check a list is empty" in {
46+
List.empty shouldBe empty
47+
}
48+
49+
it should "let us check a list is NOT empty" in {
50+
List(1, 2, 3) should not be empty
51+
}
52+
53+
it should "let us check a map contains a given key and value" in {
54+
Map('x' -> 10, 'y' -> 20, 'z' -> 30) should contain('y' -> 20)
55+
}
56+
57+
it should "let us check the type of an object" in {
58+
List(1, 2, 3) shouldBe a[List[_]]
59+
List(1, 2, 3) should not be a[Map[_, _]]
60+
}
61+
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.scala.scalatest
2+
3+
import org.scalatest.FlatSpec
4+
5+
class ListFlatSpec extends FlatSpec {
6+
7+
"An empty List" should "have size 0" in {
8+
assert(List.empty.size == 0)
9+
}
10+
11+
it should "throw an IndexOutOfBoundsException when trying to access any element" in {
12+
val emptyList = List()
13+
assertThrows[IndexOutOfBoundsException] {
14+
emptyList(1)
15+
}
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.scala.scalatest
2+
3+
import org.scalatest.FunSpec
4+
5+
class ListFunSpec extends FunSpec {
6+
7+
describe("A List") {
8+
describe("when empty") {
9+
it("should have size 0") {
10+
assert(List.empty.size == 0)
11+
}
12+
13+
it("should throw an IndexOutOfBoundsException when to access an element") {
14+
val emptyList = List()
15+
assertThrows[IndexOutOfBoundsException] {
16+
emptyList(1)
17+
}
18+
}
19+
}
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.scala.scalatest
2+
3+
import org.scalatest.FunSuite
4+
5+
class ListFunSuite extends FunSuite {
6+
7+
test("An empty List should have size 0") {
8+
assert(List.empty.size == 0)
9+
}
10+
11+
test("Accessing invalid index should throw IndexOutOfBoundsException") {
12+
val fruit = List("Banana", "Pineapple", "Apple")
13+
assert(fruit.head == "Banana")
14+
assertThrows[IndexOutOfBoundsException] {
15+
fruit(5)
16+
}
17+
}
18+
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.scala.scalatest
2+
3+
import org.scalatest.{FlatSpec, Matchers}
4+
import org.scalamock.scalatest.MockFactory
5+
6+
7+
class ScalaMockFlatSpec extends FlatSpec with MockFactory with Matchers {
8+
9+
"A mocked Foo" should "return a mocked bar value" in {
10+
val mockFoo = mock[Foo]
11+
mockFoo.bar _ expects() returning 6
12+
13+
mockFoo.bar should be(6)
14+
}
15+
}
16+
17+
class Foo {
18+
def bar = 100
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.scala.scalatest
2+
3+
import org.scalatest._
4+
import collection.mutable.ListBuffer
5+
6+
class StringFlatSpecWithBeforeAndAfter extends FlatSpec with BeforeAndAfter {
7+
8+
val builder = new StringBuilder
9+
10+
before {
11+
builder.append("Baeldung ")
12+
}
13+
14+
after {
15+
builder.clear()
16+
}
17+
18+
"Baeldung" should "be interesting" in {
19+
assert(builder.toString === "Baeldung ")
20+
21+
builder.append("is very interesting!")
22+
assert(builder.toString === "Baeldung is very interesting!")
23+
}
24+
25+
it should "have great tutorials" in {
26+
assert(builder.toString === "Baeldung ")
27+
28+
builder.append("has great tutorials!")
29+
assert(builder.toString === "Baeldung has great tutorials!")
30+
}
31+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.scala.scalatest
2+
3+
import org.scalatest.{FlatSpec, Matchers, Tag}
4+
5+
object BaeldungJavaTag extends Tag("com.baeldung.scala.scalatest.BaeldungJavaTag")
6+
7+
class TaggedFlatSpec extends FlatSpec with Matchers {
8+
9+
"Baeldung" should "be interesting" taggedAs (BaeldungJavaTag) in {
10+
"Baeldung has articles about Java" should include("Java")
11+
}
12+
13+
}

0 commit comments

Comments
 (0)