Europe: +41 78 715 83 09 - Asia: +84 ‎975 112 112


3 Feb 2012

Vaadin and Groovy Test 3

In Vaadin, an Application represents the sum of all the components organized in Windows, Layouts and having a theme applied. The central class representing an application is the com.vaadin.Application class.
His responsibilities include the following:

  • Managing windows
  • Callbacks during the lifecycle of the application
  • Setting themes

It is the main servlet of your application, where all the different screen will be call. The method init(), will start the application.

On our groovy class, we need first  launch the future application, lets call it the VaadinCalculator and add the two spock methods setup() and cleanup().

import static org.mockito.Mockito.*

class CalculatorSpec {
    def VaadinCalculator application
    def setup() {
        application = mock(VaadinCalculator)

    def cleanup() {


And now create the VaadinCalculator

import com.vaadin.Application

class VaadinCalculator extends Application{
    def init() {


Now, let write our first test case

    def whenIAdd2and4IExpect6() {

        def result = application.stored
        result == 6.0

    def void clickButton(String buttonName){
        def clickEvent = mock(Button.ClickEvent)
        def button = mock(Button)


Spoke framework, give you the possibility to write test in the format of given, when and then, to give you more visibility on what you write.

  • Line 15: I create a clickButton methods to simplify the test case on non necessary duplicate statements.
  • Line 22: we are interested only to know if the user click on specific button, we have the result so we need just call the Button.ClickListener.buttonClick and then check at line 12 if the result is correctly computed
  • Line 12: I could  use the Assertions.assertThat() from fest-assert, but  the one implemented inside Spock is quite better.

We need now just implement the methods signature and set a result default value to “6” on line 6 and now run the test, it should be a green line, so we can push our first version to our CMS

import com.vaadin.Application
import com.vaadin.ui.Button

class VaadinCalculator extends Application implements Button.ClickListener{

    String result = 6

    def void init() {


    def void buttonClick(Button.ClickEvent clickEvent) {


Now we are ready to implement our first story

Leave a Reply

Translate »