Getting started with Spock

Spock is a testing framework. You write tests in Groovy, which results in less code that is more readable. Good!

I first heard about Spock on Communities in Action and wanted to take it for a test ride. The problem was that I had some trouble getting started as the documentation seemed to be a bit outdated. Here’s a short summary of what I had to do.

The first obstacle was finding a binary download. I ended up grabbing one from their build server.

The next step is adding the spock-core-*.jar to your project and enable groovy support. (In Intellij I got a “dialog” in the top of my groovy file saying that I had to configure the Groovy SDK.)

Next up is adding a new test class and extend spock.lang.Specification. When the test is written, you can run it as a regular JUnit test.

Sweet!

Here’s a simple example

class GLMathTest extends Specification {  

  def "multiplying 4x4 matrix"() {
    given:
    float[] m1 = [
      1, 0, 0, 0,
      1, 1, 0, 0,
      1, 0, 1, 0,
      1, 0, 0, 1,
    ]
    float[] m2 = [
      1, 5, 9, 13,
      2, 6, 10, 14,
      3, 7, 11, 15,
      4, 8, 12, 16
    ]

    when:
    float[] result = GLMath.multiply4x4(m1, m2)

    then:
    result == [
      28, 5, 9, 13,
      32, 6, 10, 14,
      36, 7, 11, 15,
      40, 8, 12, 16
    ]
  }
}

Resources
Spock – http://code.google.com/p/spock/
Tutorial #1 - http://tv.jetbrains.net/videocontent/getting-started-with-spock-and-groovy
Tutorial #2 –  http://tv.jetbrains.net/videocontent/spock-and-mock-object-basics

Getting Groovy

Lately I’ve tried out the Grails framework a bit for a little pet project. It started with a little peek on Groovy, and then after a little messing around with the “getting started” part, I wanted to check out grails as well. I must say it was very pleasant to work with!

Though it was a very little project I did with Grails, I think ihas potential. I really like the way everything is done by convention, and the plugins just makes it incredible simple to have a lot of functionality up and running in no time! Security, searching and JMS just to mention a few. Check out the plugin portal if you are curious. One more thing I find cool is that you can run Grails applications on Google App Engine. This is done very easily by using a plugin!

I also noticed a project named Griffon which is a Grails like framework for developing desktop applications in Groovy. I haven’t tried it out, but it seems interesting. As I read about Griffon I also got aware of the UI toolkits SWT and Pivot. Frankly I did only know of AWT and Swing, but the next time I decide to write a GUI app in Java or Groovy, I’ll definitely consider trying out one of these :-)

Finally, some groovy and grails resources.

http://grailstutorials.com – The design is crap, but there are a lot of tutorials)
http://grailspodcast.com/blog/list – Interesting podcasts with the latest grails/groovy news
http://goo.gl/uTEM – An article on groovy meta programming (there’s a lot of groovy stuff on the same site)