收藏 分享(赏)

STS建立GWT单元测试.doc

上传人:myw993772 文档编号:7051337 上传时间:2019-05-04 格式:DOC 页数:20 大小:133.50KB
下载 相关 举报
STS建立GWT单元测试.doc_第1页
第1页 / 共20页
STS建立GWT单元测试.doc_第2页
第2页 / 共20页
STS建立GWT单元测试.doc_第3页
第3页 / 共20页
STS建立GWT单元测试.doc_第4页
第4页 / 共20页
STS建立GWT单元测试.doc_第5页
第5页 / 共20页
点击查看更多>>
资源描述

1、STS 建立 GWT 单元测试1. 新建 Maven 项目:FileOther MavenMaven Project。Group Id:cn.mycompany.gwt Artifact Id:testgwt Packing:warNextAdd全选NextFinish 。2. 新建类 Calculator:NewGooglipseModul。Packing:cn.mycompany.gwt Name:UiBinderPracticepublic void onModuleLoad() RootPanel rootPanel = RootPanel.get();Calculator calc

2、= new Calculator(“bob“, “sally“, “fred“);CalculatorPresenter presenter=new CalculatorPresenter();presenter.setDisplay(calc);rootPanel.add(calc);3.新建类: CalculatorPresenter。enum OperationsADD,SUBTRACT,MULTIPLY,DIVIDE,RECIVING;private Operations currentOperation=null;private double accumulator=0.0;priv

3、ate int flag=0;private Display display=null;private HasText result=null;/* this method binds event handlers to all controls.*/private void bind()result = display.getResult();display.getBtn0().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) appendDigit(0););display.getBtn1().

4、addClickHandler(new ClickHandler() public void onClick(ClickEvent event) appendDigit(1););display.getBtn2().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) appendDigit(2););display.getBtn3().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) appendDigit

5、(3););display.getBtn4().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) appendDigit(4););display.getBtn5().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) appendDigit(5););display.getBtn6().addClickHandler(new ClickHandler() public void onClick(Click

6、Event event) appendDigit(6););display.getBtn7().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) appendDigit(7););display.getBtn8().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) appendDigit(8););display.getBtn9().addClickHandler(new ClickHandler() p

7、ublic void onClick(ClickEvent event) appendDigit(9););display.getClear().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) clearDisplay(););display.getAddBtn().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) add(););display.getEqualsBtn().addClickHandl

8、er(new ClickHandler() public void onClick(ClickEvent event) doEquals(););display.getMinusBtn().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) minus(););display.getMultBtn().addClickHandler(new ClickHandler() public void onClick(ClickEvent event) mult(););display.getDivBtn()

9、.addClickHandler(new ClickHandler() public void onClick(ClickEvent event) div(););private void appendDigit(int digit)if(currentOperation=null)currentOperation=Operations.RECIVING;result.setText(“);result.setText(result.getText() +digit);/* */private void div()double value =getValue();doOperationWith

10、(value);currentOperation=Operations.DIVIDE;clearDisplay();private void mult()double value =getValue();doOperationWith(value);currentOperation=Operations.MULTIPLY;clearDisplay();private void minus()double value =getValue();doOperationWith(value);currentOperation=Operations.SUBTRACT;clearDisplay();pri

11、vate void doEquals()double value=getValue();System.out.println(“3value-“+value);doOperationWith(value);if(flag!=0)result.setText(“DIV ERROR!“);elseresult.setText(“+accumulator);currentOperation=null;private void add()double value =getValue();doOperationWith(value);currentOperation=Operations.ADD;cle

12、arDisplay();private void doOperationWith(double value)if(currentOperation!=nullif(currentOperation=Operations.ADD)accumulator+=value;if(currentOperation=Operations.SUBTRACT)accumulator-=value;if(currentOperation=Operations.MULTIPLY)accumulator*=value;if(currentOperation=Operations.DIVIDE)System.out.

13、println(“5value-“+value);System.out.println(“4result.getText()-“+result.getText();if(result.getText().equals(“0“)System.out.println(“6value-“+value);flag=1;elseSystem.out.println(“4result.getText()-“+result.getText();accumulator/=value;elseaccumulator=value;/* Get value from display* return*/protect

14、ed double getValue()String resultvalue=result.getText();System.out.println(“1resultvalue-“+resultvalue);double value=Double.parseDouble(resultvalue);System.out.println(“2value-“+value);return value;private void clearDisplay()result.setText(“);public interface DisplayHasText getResult();HasClickHandl

15、ers getBtn0();HasClickHandlers getBtn1();HasClickHandlers getBtn2();HasClickHandlers getBtn3();HasClickHandlers getBtn4();HasClickHandlers getBtn5();HasClickHandlers getBtn6();HasClickHandlers getBtn7();HasClickHandlers getBtn8();HasClickHandlers getBtn9();HasClickHandlers getAddBtn();HasClickHandle

16、rs getMinusBtn();HasClickHandlers getMultBtn();HasClickHandlers getDivBtn();HasClickHandlers getClear();HasClickHandlers getEqualsBtn();public void setDisplay(Display display)this.display=display;bind();4.新建类 Calculator:public class Calculator extends Composite implements CalculatorPresenter.Display

17、interface MyUiBinder extends UiBinderprivate static MyUiBinder uiBinder=GWT.create(MyUiBinder.class);UiField TextBox result;UiField Button btn0;UiField Button btn1;UiField Button btn2;UiField Button btn3;UiField Button clearBtn;UiField Button btn4;UiField Button btn5;UiField Button btn6;UiField Butt

18、on addBtn;UiField Button btn7;UiField Button btn8;UiField Button btn9;UiField Button minusBtn;UiField Button multBtn;UiField Button divBtn;UiField Button equalsBtn;public Calculator(String. names)initWidget(uiBinder.createAndBindUi(this);public HasText getResult() / TODO Auto-generated method stubre

19、turn result;public HasClickHandlers getBtn0() / TODO Auto-generated method stubreturn btn0;public HasClickHandlers getBtn1() / TODO Auto-generated method stubreturn btn1;public HasClickHandlers getBtn2() / TODO Auto-generated method stubreturn btn2;public HasClickHandlers getBtn3() / TODO Auto-gener

20、ated method stubreturn btn3;public HasClickHandlers getBtn4() / TODO Auto-generated method stubreturn btn4;public HasClickHandlers getBtn5() / TODO Auto-generated method stubreturn null;public HasClickHandlers getBtn6() / TODO Auto-generated method stubreturn null;public HasClickHandlers getBtn7() /

21、 TODO Auto-generated method stubreturn null;public HasClickHandlers getBtn8() / TODO Auto-generated method stubreturn null;public HasClickHandlers getBtn9() / TODO Auto-generated method stubreturn null;public HasClickHandlers getAddBtn() return addBtn;public HasClickHandlers getMinusBtn() / TODO Aut

22、o-generated method stubreturn null;public HasClickHandlers getMultBtn() / TODO Auto-generated method stubreturn null;public HasClickHandlers getDivBtn() / TODO Auto-generated method stubreturn null;public HasClickHandlers getEqualsBtn() / TODO Auto-generated method stubreturn null;public HasClickHan

23、dlers getClear() return clearBtn;5.新建 Calcula.ui.xml;0123C456+789-*/=6.在 src/test/java 下建立 CalculatorPresenterTestNewJunit Test Casepublic class CalculatorPresenterTest implements CalculatorPresenter.Displayprivate CalculatorPresenter instance = null;private MockHasClickHandlers mockB1 = null;privat

24、e MockHasText mockResult = null;MockHasClickHandlers mockB2 = null;MockHasClickHandlers mockB3 = null;MockHasClickHandlers mockB4 = null;MockHasClickHandlers mockB5 = null;MockHasClickHandlers mockB6 = null;MockHasClickHandlers mockB7 = null;MockHasClickHandlers mockB8 = null;MockHasClickHandlers mo

25、ckB9 = null;MockHasClickHandlers mockB0 = null;MockHasClickHandlers mockBClear = null;MockHasClickHandlers mockAdd = null;MockHasClickHandlers mockEquals = null;MockHasClickHandlers mockMinus=null;MockHasClickHandlers mockMult=null;MockHasClickHandlers mockDiv=null;Beforepublic void setUp() throws E

26、xception mockB1 = new MockHasClickHandlers();mockB2 = new MockHasClickHandlers();mockB3 = new MockHasClickHandlers();mockB4 = new MockHasClickHandlers();mockB5 = new MockHasClickHandlers();mockB6 = new MockHasClickHandlers();mockB7 = new MockHasClickHandlers();mockB8 = new MockHasClickHandlers();moc

27、kB9 = new MockHasClickHandlers();mockB0 = new MockHasClickHandlers();mockBClear= new MockHasClickHandlers();mockResult = new MockHasText();mockAdd=new MockHasClickHandlers();mockEquals=new MockHasClickHandlers();mockMinus=new MockHasClickHandlers();mockMult=new MockHasClickHandlers();mockDiv=new Moc

28、kHasClickHandlers();instance = new CalculatorPresenter();instance.setDisplay(this);Afterpublic void tearDown() throws Exception instance = null;mockB1 = null;mockB2 = null;mockB3 = null;mockB4 = null;mockB5 = null;mockB6 = null;mockB7 = null;mockB8 = null;mockB9 = null;mockB0 = null;mockBClear=null;

29、mockAdd=null;mockResult = null; mockEquals=null;mockMinus=null;mockMult=null;mockDiv=null;/-Test Start-Testpublic void testPress0() mockB0.click();assertEquals(“0 not pressed“,“0“,mockResult.getText();Testpublic void testPress1() mockB1.click();assertEquals(“1 not pressed“,“1“,mockResult.getText();T

30、estpublic void testPress2() mockB2.click();assertEquals(“2 not pressed“,“2“,mockResult.getText();Testpublic void testPress3() mockB3.click();assertEquals(“3 not pressed“,“3“,mockResult.getText();Testpublic void testPress4() mockB4.click();assertEquals(“4 not pressed“,“4“,mockResult.getText();Testpub

31、lic void testPress5() mockB5.click();assertEquals(“5 not pressed“,“5“,mockResult.getText();Testpublic void testPress6() mockB6.click();assertEquals(“6 not pressed“,“6“,mockResult.getText();Testpublic void testPress7() mockB7.click();assertEquals(“7 not pressed“,“7“,mockResult.getText();Testpublic vo

32、id testPress8() mockB8.click();assertEquals(“8 not pressed“,“8“,mockResult.getText();Testpublic void testPress9() mockB9.click();assertEquals(“9 not pressed“,“9“,mockResult.getText();Testpublic void testClear() /enter 123mockB1.click();mockB2.click();mockB3.click();assertEquals(“123 not pressed“,“12

33、3“,mockResult.getText();/press button clear buttonmockBClear.click();assertEquals(“Display not Cleared“,“,mockResult.getText();Testpublic void testAdd() /enter 23mockB2.click();mockB3.click();assertEquals(“23 not pressed“,“23“,mockResult.getText();/press button add buttonmockAdd.click();/enter 45moc

34、kB4.click();mockB5.click();assertEquals(“45 not pressed“,“45“,mockResult.getText();/press button equals button mockEquals.click();assertEquals(“value incorect“,“68.0“,mockResult.getText();Testpublic void testgetValue() mockResult.setText(“45.3“);assertEquals(“45.3 not pressed“,45.3,instance.getValue

35、(),0.0);Testpublic void testAddMany() /enter 23mockB2.click();mockB3.click();assertEquals(“23 not pressed“,“23“,mockResult.getText();/press button add button mockAdd.click();/enter 45mockB4.click();mockB5.click();assertEquals(“45 not pressed“,“45“,mockResult.getText();/press button add buttonmockAdd

36、.click();/enter 11mockB1.click();mockB1.click();assertEquals(“11 not pressed“,“11“,mockResult.getText();/press button equals button mockEquals.click();assertEquals(“value incorect“,“79.0“,mockResult.getText();Testpublic void testMinus() /enter 23mockB2.click();mockB3.click();assertEquals(“23 not pre

37、ssed“,“23“,mockResult.getText();/press button minus buttonmockMinus.click();/enter 12mockB1.click();mockB2.click();assertEquals(“12 not pressed“,“12“,mockResult.getText();/press button equals button mockEquals.click();assertEquals(“value incorect“,“11.0“,mockResult.getText();Testpublic void testAdda

38、ndMinusMany() /enter 23mockB2.click();mockB3.click();assertEquals(“23 not pressed“,“23“,mockResult.getText();/press button add button mockAdd.click();/enter 45mockB4.click();mockB5.click();assertEquals(“45 not pressed“,“45“,mockResult.getText();/here 68/press button add buttonmockAdd.click();/enter

39、11mockB1.click();mockB1.click();assertEquals(“11 not pressed“,“11“,mockResult.getText();/here 79mockMinus.click();/enter 52mockB5.click();mockB2.click();assertEquals(“52 not pressed“,“52“,mockResult.getText();/here 27mockMinus.click();/enter 14mockB1.click();mockB4.click();assertEquals(“14 not press

40、ed“,“14“,mockResult.getText();/here 13mockMinus.click();/enter 18mockB1.click();mockB8.click();assertEquals(“14 not pressed“,“18“,mockResult.getText();/here -5/press button equals button mockEquals.click();assertEquals(“value incorect“,“-5.0“,mockResult.getText();Testpublic void testBug1() /enter 1m

41、ockB1.click();assertEquals(“1 not pressed“,“1“,mockResult.getText();/press button add button mockAdd.click();/enter 3mockB3.click();assertEquals(“3 not pressed“,“3“,mockResult.getText();/here 4/press button equals button mockEquals.click();assertEquals(“value incorect“,“4.0“,mockResult.getText();/en

42、ter 222mockB2.click();mockB2.click();mockB2.click();assertEquals(“222 not pressed“,“222“,mockResult.getText();Testpublic void testMult() /enter 2mockB3.click();assertEquals(“3 not pressed“,“3“,mockResult.getText();/press button minus buttonmockMult.click();/enter 2mockB6.click();assertEquals(“6 not

43、pressed“,“6“,mockResult.getText();/press button equals button mockEquals.click();assertEquals(“value incorect“,“18.0“,mockResult.getText();Testpublic void testDiv() /enter 8mockB8.click();assertEquals(“8 not pressed“,“8“,mockResult.getText();/press button minus buttonmockDiv.click();/enter 2mockB2.c

44、lick();assertEquals(“2 not pressed“,“2“,mockResult.getText();/press button equals button mockEquals.click();assertEquals(“value incorect“,“4.0“,mockResult.getText();Testpublic void testDivz() /enter 2mockB3.click();assertEquals(“3 not pressed“,“3“,mockResult.getText();/press button minus buttonmockM

45、ult.click();/enter 2mockB0.click();assertEquals(“0 not pressed“,“0“,mockResult.getText();/press button equals button mockEquals.click();assertEquals(“value incorect“,“0.0“,mockResult.getText();/-Test End-/ helper classes /private class MockHasText implements HasText String text = “;public String get

46、Text() return text;public void setText(String text) this.text = text;private class MockHasClickHandlers implements HasClickHandlers private Set handlers = new HashSet();private void click()ClickEvent clickEvent = new ClickEvent();this.fireEvent(clickEvent);public void fireEvent(GwtEvent event) for (

47、ClickHandler handler : handlers)handler.onClick(ClickEvent) event);public HandlerRegistration addClickHandler(ClickHandler handler) handlers.add(handler);return null;public HasText getResult() return mockResult;public HasClickHandlers getBtn0() return mockB0;public HasClickHandlers getBtn1() return

48、mockB1;public HasClickHandlers getBtn2() return mockB2;public HasClickHandlers getBtn3() return mockB3;public HasClickHandlers getBtn4() return mockB4;public HasClickHandlers getBtn5() return mockB5;public HasClickHandlers getBtn6() return mockB6;public HasClickHandlers getBtn7() return mockB7;public HasClickHandlers getBtn8() return mockB8;public HasClickHandlers getBtn9() return mockB9;public HasClickHandler

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报