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.

package ch.ge.vaadin
import com.vaadin.Application
import com.vaadin.ui.Button

class VaadinCalculator extends Application implements Button.ClickListener{

static String TWO = "2"
String result = 6

@Override
def void init() {

}

def void buttonClick(Button.ClickEvent clickEvent) {
def button = clickEvent.button
result = button.caption
}
}

For the readers who don’t use TDD, it’s quite strange to implement only the button “2” and not all the button. But we will by the time we have tested all the button. If we run the current test, now the test, it’s green.

On line 17th and 18th, we have all the properties needed to catch the information needed. Exactly now, the page display nothing. But the event work perfectly.

Advertisements