My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
QuickStart  
Your first specification starts here!
Featured
Updated Feb 5, 2010 by etorrebo...@gmail.com

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

  1. declare a Specification class
  2. import org.specs._
    
    class helloWorld extends Specification
  3. a Specification is just a list of examples:
  4. import org.specs._
    
    class 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._
    
    class 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 run 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 add more structure to your specification, by grouping your examples:
  10. import org.specs._
    
    class 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 extends the SpecificationWithJUnit class to run your specification inside Eclipse for example (using the JUnit 4.7 library):
  12. class helloWorldTest extends SpecificationWithJUnit { ... }

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

Comment by TChampen...@gmail.com, Nov 5, 2009

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

Comment by robertku...@gmail.com, 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) ...

Comment by brstr...@gmail.com, Mar 18, 2010

Robert: this tripped me up too a while back. I'm not sure which shell derivative OS X uses, but in bash, the "~" character does not expand to your home directory when embedded, eg:

$ echo ~ /home/YourUserName? $ echo .:~ .:~

Spell out the full path and it should work, eg "scala -cp .:/home/YourUserName?/work/..."

Comment by brstr...@gmail.com, Mar 18, 2010

Let's try again, now with wiki-fu at the ready:

$ echo ~
/home/YourUserName
$ echo .:~
.:~
Comment by grimmfr...@gmail.com, Apr 28, 2010

I created a Scala class which extends SpecificationWithJUnit. When I try to run it with Eclipse's JUnit runner (Run As > JUnit Test), no tests are found and a message is shown: No tests found with test runner 'JUnit 4'.

The project references junit-4.7.jar. Do I have to add a JUnit test suite (by means of Java code) in order to make JUnit aware of the Scala test classes?

Comment by newca12, May 26, 2010

grimmfrank : check this http://www.scala-lang.org/node/363

Comment by ked...@gmail.com, Jun 15, 2010

> compile and run the class named helloWorld: scala -cp specs-<version>.jar run helloWorld:

Add ".:" to the classpath there to avoid tearing your hair out.

Comment by chris.ve...@gmail.com, Dec 29, 2010

No luck with scala 2.8.1.final, java 1.6.0_20 and specs_2.8.1-1.6.6

scala -cp specs_2.8.1-1.6.6.jar:. run helloWorld.scala

Gives no output whatsoever. No errors, just nothing.

Comment by chris.ve...@gmail.com, Dec 29, 2010

Solution: scalac -cp specs_2.8.1-1.6.6.jar helloWorld.scala scala -cp specs_2.8.1-1.6.6.jar:. run helloWorld

Does work of course, the instructions seemed to imply it would run as a script.

Comment by ryan.met...@gmail.com, Jan 7, 2011

Here's a quick bash script I've hacked together. Just update the SPECSJAR var to your path and version

SPECSJAR=/my/path/to/specs/specs_2.8.1-1.6.6.jar

#!/bin/bash

NUMPARAMS=2
SPECSJAR=/usr/local/lib/specs/specs_2.8.1-1.6.6.jar

if [ $# -lt "$NUMPARAMS" ]
then
	echo

	echo "This script requires two parameters!"
	echo "For example: ./runtest helloWorld.scala helloWorld"

else
	echo
	echo "My code's compiling."
	echo "COMPILING!"
	echo
	echo "Oh. Carry on"
	echo

	echo "(Running) scalac -cp $SPECSJAR $1"
	scalac -cp $SPECSJAR $1
	echo "(Running) scala -cp $SPECSJAR:. run $2"
	scala -cp $SPECSJAR:. run $2
fi

Sign in to add a comment
Powered by Google Project Hosting