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
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) application.init() } def cleanup() { application.close() } }
And now create the VaadinCalculator
import com.vaadin.Application class VaadinCalculator extends Application{ def init() { } }Now, let write our first test case
def whenIAdd2and4IExpect6() { when: clickButton("C") clickButton("2") clickButton("+") clickButton("4") clickButton("=") then: def result = application.stored result == 6.0 } def void clickButton(String buttonName){ def clickEvent = mock(Button.ClickEvent) def button = mock(Button) when(button.getCaption()).thenReturn(buttonName) when(clickEvent.button).thenReturn(button) application.buttonClick(clickEvent) }
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.
import com.vaadin.Application import com.vaadin.ui.Button class VaadinCalculator extends Application implements Button.ClickListener{ String result = 6 @Override def void init() { } def void buttonClick(Button.ClickEvent clickEvent) { }Now we are ready to implement our first story
No comments:
Post a Comment