The basics of mocking in Vaadin
In this recipe, we will show how to write a test for code which is not designed for easy unit testing. For example, we are forced to use an external class, which contains only a static method returning the status of a system. We just return a plain and hardcoded string "Online" in this example. But the class could, for example, return a status of a system, which is fetched from a web service.
public class SystemStatusService { public static String getValue() { return "Offline"; } }
We create a horizontal layout on which we place a label that contains a system status we get from the service:
public class SystemStatusLayout extends HorizontalLayout { private Label lblSystemStatus; public SystemStatusLayout() { String value = SystemStatusService.getValue(); lblSystemStatus = new Label(value); addComponent(lblSystemStatus); } public Label getLblSystemStatus() { return lblSystemStatus; } }
Some...