4.8.1. 单元测试

可以在中间件和客户端这两创建和执行单元测试用例。平台集成了 JUnitJMockit 用来做单元测试。

假设有下面这个界面控制器:

  1. public class OrderEditor extends AbstractEditor {
  2. @Named("itemsTable.add")
  3. protected AddAction addAction;
  4. @Override
  5. public void init(Map<String, Object> params) {
  6. addAction.setWindowId("sales$Product.browse");
  7. addAction.setHandler(new Lookup.Handler() {
  8. @Override
  9. public void handleLookup(Collection items) {
  10. // some code
  11. }
  12. });
  13. }
  14. }

可以编写以下测试类来检查 init() 方法:

  1. public class OrderEditorTest {
  2. OrderEditor editor;
  3. @Mocked
  4. Window.Editor frame;
  5. @Mocked
  6. AddAction addAction;
  7. @Before
  8. public void setUp() throws Exception {
  9. editor = new OrderEditor();
  10. editor.setWrappedFrame(frame);
  11. editor.addAction = addAction;
  12. }
  13. @Test
  14. public void testInit() {
  15. editor.init(Collections.<String, Object>emptyMap());
  16. editor.setItem(new Order());
  17. new Verifications() {
  18. {
  19. addAction.setWindowId("sales$Product.browse");
  20. addAction.setHandler(withInstanceOf(Window.Lookup.Handler.class));
  21. }
  22. };
  23. }
  24. }