《测试驱动开发-实用指南》第八章的TestWidgets,用基于-swt编程,
下面看代码:
import org.easymock.MockControl;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import junit.framework.TestCase;
/*
* 创建日期 2004-10-20
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class TestSWTWidgets extends TestCase {
private MockControl control=null;
private MovieListEditor mockEditor=null;
private SWTMovieListEditorView editorView=null;
public static void main(String[] args) {
junit.textui.TestRunner.run(TestSWTWidgets.class);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
control=MockControl.createControl(MovieListEditor.class);
mockEditor=(MovieListEditor) control.getMock();
control.replay();
editorView=new SWTMovieListEditorView(mockEditor);
editorView.show();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
public void testList() {
List movieList=editorView.getMovieList();
assertNotNull("Movie list should be non null",movieList);
assertTrue("Movie list shoud be showing",movieList.isVisible());
}
public void testField() {
Text movieField=editorView.getMovieField();
assertNotNull("Movie field should be non null",movieField);
assertTrue("Movie field should be showing",movieField.isVisible());
}
public void testAddButton() {
Button addButton=editorView.getAddButton();
assertNotNull("Movie addButton should be non null",addButton);
assertTrue("Movie addButton should be showing",addButton.isVisible());
assertEquals("Add button should be label","Add",addButton.getText());
}
public void testDeleteButton() {
Button deleteButton=editorView.getDeleteButton();
assertNotNull("Movie deleteButton should be non null",deleteButton);
assertTrue("Movie deleteButton should be showing",deleteButton.isVisible());
assertEquals("Delete button should be label","Add",deleteButton.getText());
}
}
下面是实际生成的swt-gui:
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
/*
* 创建日期 2004-10-20
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class SWTMovieListEditorView {
private org.eclipse.swt.widgets.Shell sShell = null;
private List list=null;
private Button addButton=null;
private Button deleteButton=null;
private Text movieField=null;
private MovieListEditor listEditor=null;
/**
* @param mockEditor
*/
public SWTMovieListEditorView(MovieListEditor anEditor) {
listEditor=anEditor;
}
/**
* This method initializes sShell
*/
private void createSShell() {
sShell = new org.eclipse.swt.widgets.Shell();
addButton = new Button(sShell, SWT.NONE);
deleteButton = new Button(sShell, SWT.NONE);
movieField = new Text(sShell, SWT.NONE);
sShell.setSize(new org.eclipse.swt.graphics.Point(300,200));
sShell.setText("Shell");
addButton.setBounds(new org.eclipse.swt.graphics.Rectangle(55,124,71,16));
addButton.setText("add");
deleteButton.setBounds(new org.eclipse.swt.graphics.Rectangle(143,125,85,16));
deleteButton.setText("delete");
movieField.setBounds(new org.eclipse.swt.graphics.Rectangle(55,50,164,39));
}
/**
*
*/
public void show() {
this.createSShell();
}
/**
* @return
*/
public List getMovieList() {
// TODO 自动生成方法存根
return null;
}
/**
* @return
*/
public Text getMovieField() {
// TODO 自动生成方法存根
return movieField;
}
/**
* @return
*/
public Button getAddButton() {
// TODO 自动生成方法存根
return addButton;
}
/**
* @return
*/
public Button getDeleteButton() {
// TODO 自动生成方法存根
return deleteButton;
}
}