Follow the installation instructions and create the following specification in a file named HelloWorldSpec.scala
:
import org.specs2.*
class HelloWorldSpec extends Specification:
def is = s2"""
This is a specification to check the 'Hello world' string
The 'Hello world' string should
contain 11 characters $e1
start with 'Hello' $e2
end with 'world' $e3
"""
def e1 = "Hello world" must haveSize(11)
def e2 = "Hello world" must startWith("Hello")
def e3 = "Hello world" must endWith("world")
A org.specs2.Specification
and declaring an is
method. That method defines a s2
interpolated string with some plain text describing what the system should do and some code with executable examples.
The style of writing specifications above, with most of the text first, then executable examples, is unconventional. You can, if you prefer, use an alternative style:
// note the different import here
import org.specs2.mutable.*
class HelloWorldSpec extends Specification:
"This is a specification to check the 'Hello world' string".br
"The 'Hello world' string should" >> {
"contain 11 characters" >> {
"Hello world" must haveSize(11)
}
"start with 'Hello'" >> {
"Hello world" must startWith("Hello")
}
"end with 'world'" >> {
"Hello world" must endWith("world")
}
}
Both specifications will produce the same output.
And this is it! Now you can execute your specification with a runner and observe the results:
sbt> testOnly *HelloWorldSpec
[info] HelloWorldSpec
[info]
[info] This is a specification to check the 'Hello world' string
[info]
[info] The 'Hello world' string should
[info] + contain 11 characters
[info] + start with 'Hello'
[info] + end with 'world'
[info]
[info] Total for specification HelloWorldSpec
[info] Finished in 0 second, 58 ms
[info] 3 examples, 0 failure, 0 error
The rest of this User Guide will show you how to: