Alexandre Cuva

Monday, February 20, 2012

Vaadin and Groovy Test 5

Moving from the test, the implementation and the re-factoring we get the following results
package ch.ge.vaadin

import org.junit.Test
import org.junit.Before
import org.junit.After
import com.vaadin.ui.Button
import static org.mockito.Mockito.*

class VaadinCalculatorTest {

    private VaadinCalculator calculator
    
    @Before void before() {
        calculator = new VaadinCalculator()
        calculator.init()
    }
    
    @After void after() {
        calculator.close()
    }
    
    @Test void shouldDisplay2IfIPressButton2() {
        def event = mock(Button.ClickEvent)
        def button = mock(Button)
        
        when(button.getCaption()).thenReturn("2")
        when(event.button).thenReturn(button)

        calculator.buttonClick(event)

        assert calculator.stored == 0.0
        assert calculator.current == 2.0
    }

    @Test void shouldDisplay2IfIPressButton2AndPlus() {
        def event = mock(Button.ClickEvent)
        def button = mock(Button)

        when(button.getCaption()).thenReturn("2")
        when(event.button).thenReturn(button)

        calculator.buttonClick(event)

        when(button.getCaption()).thenReturn("+")
        calculator.buttonClick(event)

        assert calculator.current == 0.0
        assert calculator.stored == 2.0

    }

    @Test void shouldDisplay4IfIPressButton2AndPlusAnd4() {
        def event = mock(Button.ClickEvent)
        def button = mock(Button)

        when(button.getCaption()).thenReturn("2")
        when(event.button).thenReturn(button)

        calculator.buttonClick(event)

        when(button.getCaption()).thenReturn("+")
        calculator.buttonClick(event)

        when(button.getCaption()).thenReturn("4")
        calculator.buttonClick(event)

        assert calculator.current == 4.0
        assert calculator.stored == 2.0

    }

    @Test void shouldDisplay6IfIPressButton2AndPlusAnd4AndEqual() {
        def event = mock(Button.ClickEvent)
        def button = mock(Button)

        when(button.getCaption()).thenReturn("2")
        when(event.button).thenReturn(button)

        calculator.buttonClick(event)

        when(button.getCaption()).thenReturn("+")
        calculator.buttonClick(event)

        when(button.getCaption()).thenReturn("4")
        calculator.buttonClick(event)

        when(button.getCaption()).thenReturn("=")
        calculator.buttonClick(event)

        assert calculator.current == 0.0
        assert calculator.stored == 6.0
    }

}

Wednesday, February 15, 2012

Groovy Spock and Maven 3.0

Here you can found a new version of HelloSpockPom for Maven 3.0. With Maven 3.0, you don't need anymore the Spock-maven plugin. And so you can use the last artifacts like bellow
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>
 <groupId>hello</groupId>
 <artifactId>spock</artifactId>
 <version>1.0</version>
 <packaging>jar</packaging>
 <name>Hello Spock</name>

 <build>
  <plugins>
   <plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.3</version>
    <configuration>
     <providerSelection>1.8</providerSelection>
     <source/>
    </configuration>
    <executions>
     <execution>
      <goals>
       <goal>testCompile</goal>
       <goal>compile</goal>
      </goals>
     </execution>
    </executions>
    <dependencies>
     <dependency>
      <groupId>org.codehaus.gmaven.runtime</groupId>
      <artifactId>gmaven-runtime-1.8</artifactId>
      <version>1.4</version>
      <exclusions>
       <exclusion>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
       </exclusion>
      </exclusions>
     </dependency>
     <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>1.8.5</version>
     </dependency>
    </dependencies>
   </plugin>
  </plugins>
 </build>

 <dependencies>
  <dependency>
   <groupId>org.spockframework</groupId>
   <artifactId>spock-core</artifactId>
   <version>0.5-groovy-1.8</version>
   <scope>test</scope>
   <exclusions>
    <exclusion>
     <groupId>org.codehaus.groovy</groupId>
     <artifactId>groovy-all</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.codehaus.groovy</groupId>
   <artifactId>groovy-all</artifactId>
   <version>1.8.5</version>
  </dependency>
 </dependencies>
</project>

Monday, February 6, 2012

Vaadin and Groovy Test 4

TDD for Story 1
Now we have wrote the TA for the first Story, we can start on the TDD.
I create a class call VaadinCalculatorTest
package ch.ge.vaadin

import org.junit.Test
import org.junit.Before
import org.junit.After
import com.vaadin.ui.Button
import static org.mockito.Mockito.*

class VaadinCalculatorTest {

    private VaadinCalculator calculator
    
    @Before void before() {
        calculator = new VaadinCalculator()
        calculator.init()
    }
    
    @After void after() {
        calculator.close()
    }
    
    @Test void shouldDisplay2IfIPressButton2() {
        def event = mock(Button.ClickEvent)
        def button = mock(Button)
        
        when(button.getCaption()).thenReturn(VaadinCalculator.TWO)
        when(event.button).thenReturn(button)

        calculator.buttonClick(event)

        assert calculator.result == VaadinCalculator.TWO

    }

}

The first thing, we need to verify it's if press button "2", it's what we pressed. It's quite similar to our VaadinCalculatorSpec. If we run now, the compilation  work but the test fail. We need to implement the VaadinCalculator so the test run green.

Thursday, February 2, 2012

Vaadin and Groovy Test 3

CalculatorSpec.groovy
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().

Vaadin and Groovy Test 2

Yesterday we created our first Vaadin application with Maven. Today as a true Agile Developer we will try to work with Vaadin with our testing tools.

Work on Acceptance Test
We will work on a simple calculator, with 9 buttons for the digits, 4 buttons for the operator (/, *, -, +) and an enter button, to display the result.
The first stories would be :

  • As a User, when I push the button 2, +, 4, I expect have a result of 6
  • As a User, I expect to see a classic calculator with the numbers in a grid of 3 x 3

Wednesday, February 1, 2012

Vaadin and Groovy Test


I had the chance to see a presentation of Vaadin with Nicolas Fränkel, who also write an interesting book "Learning Vaadin". Vaadin is a new Java web framework for making applications look great and perform well, making your users happy. He promises to make your user interfaces attractive and usable while easing your development effort and boosting your productivity.
Vaadin change the way you write web application by thinking application and events process. That's a great news, write a Swing application or a web application will be the same. No more changing the way to write you app. So I grab a book edition and run on my computer to try Vaddin.


Creating a Maven Project
Before starting our Vaadin project you need to create a Maven project with the Vaadin Maven archetype as explain on Using Vaadin with Maven

mvn archetype:generate
-DarchetypeGroupId=com.vaadin
-DarchetypeArtifactId=vaadin-archetype-clean
-DarchetypeVersion=LATEST
-DgroupId=your.company 
-DartifactId=project-name
-Dversion=1.0
-Dpackaging=war

Running the Application
Your project should be ready now and you can launch a testing application with mvn jetty:run and then open your browser under http://localhost:8080/.

The pom has everything ready to work on your Vaadin application.