My favorites | Sign in
Logo
                
Search
for
Updated May 07, 2009 by etorreborre
Labels: Featured
QuickStart  
Your first specification starts here!

(for more information, take a look at the User Guide)

  1. declare a Specification object
  2. import org.specs._
    
    object helloWorld extends Specification
  3. a Specification is just a list of examples:
  4. import org.specs._
    
    object helloWorld extends Specification {
      "'hello world' has 11 characters" in {}
      "'hello world' matches 'h.* w.*'" in {}
    }
  5. back-up your examples with expectations:
  6. import org.specs._
    
    object helloWorld extends Specification {
      "'hello world' has 11 characters" in {
         "hello world".size must_== 11
      }
      "'hello world' matches 'h.* w.*'" in {
         "hello world" must be matching("h.* w.*")
      }
    }
  7. compile and run the class named helloWorld: scala -cp specs-<version>.jar helloWorld
  8. Specification "helloWorld"
      specifies 
      + 'hello world' has 11 characters
      + 'hello world' matches 'h.* w.*'
    
    Total for specification "helloWorld":
    Finished in 0 second, 63 ms
    2 examples, 2 assertions, 0 failure, 0 error
  9. now, you can more structure to your specification, by grouping your examples:
  10. import org.specs._
    
    object helloWorld extends Specification {
      "hello world" should {
        "have 11 characters" in {
          "hello world".size must_== 11
        }
        "match 'h.* w.*'" in {
          "hello world" must be matching("h.* w.*")
        }
      }
      "Good bye cruel world" should {...}
    }
  11. You can also use a JUnit runner to run your specification inside Eclipse for example (using the JUnit 4.5 library). Add:
  12. import org.specs.runner._
    class helloWorldTest extends JUnit4(helloWorld)
    
    // OR declare your specification as a class and mix-in the JUnit trait
    // in that case, the specification can still be run on the command line with
    // `scala -cp specs-<version>.jar run helloWorld` 
    class helloWorld extends Specification with JUnit { ... }
    

Note: helloWorldTest is declared as a class and not an object to be instantiated properly by JUnit runners.


Comment by TChampenier, Nov 05, 2009

5. now, you can add more structure to your specification

Comment by robertkuhar, Nov 16, 2009

No joy getting helloWorld Specification to run: specs1.6.1, scala 2.7.7.final, java1.6.0_15 on OS X

scalac -cp ~/work/specs1.6.1/specs-1.6.1.jar helloWorld.scala scala -cp .:~/work/specs1.6.1/specs-1.6.1.jar helloWorld Exception in thread "main" java.lang.NoClassDefFoundError??: org/specs/matcher/Matcher

at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2427) ...
Caused by: java.lang.ClassNotFoundException??: org.specs.matcher.Matcher
at java.net.URLClassLoader$1.run(URLClassLoader.java:200) ...


Sign in to add a comment
Hosted by Google Code