Simulation object (2)

xiaoxiao2021-03-05  20

In the previous one, I briefly introduced Mock. Today I will explain how to test the GUI with Easymock.

For the installation of Easymock, see its description documentation, this is no longer introduced.

Before conducting tests, we must have a problem first to solve: That is how to get the various components in the GUI in the test code? Maybe you will say that you can define them as public, but this is not very compliant with the object-oriented packaging ideas. Reflection? It is also good, but it seems too complicated. It seems to be no need for a test. Of course we can take advantage of other auxiliary test tools, such as JFCUnit, which will be introduced in the next. Now, suppose we don't know other tools with third parties, what should I do? My approach is to borrow JavaBean's ideas - provide a get method.

Ok, let's take a specific operation below.

First, we assume that the GUI to test is defined an interface, simple point, as shown in:

Package testgui;

Import java.util. *;

Public interface listeditor {

Vector getNames ();

Void Add (String Name);

Void delete (int index);

}

Then we can write the test program (if you use TDD, we just want to demonstrate the use of easymock in the test of GUI, so we don't do this, we write the GUI code first, as follows:

Package testgui;

Import javax.swing. *;

Import java.util. *;

Import java.awt. *;

Import java.awt.event. *;

Public Class ListWindow

Extends jframe {

Private jlist namelist;

Private jbutton addbutton;

Private listeditor myEditor;

Private JTextField Namefield;

Public ListWindow (listeditor myEeditor) {

Super ();

THIS.MYEDITOR = myEditor;

}

Public jlist getnamelist () {

Return namelist;

}

Public JButton getAddButton () {

Return addbutton;

}

Public JtextField GetNameField () {

Return Namefield;

}

Public void init () {

setLayout ();

Initlist ();

INITDBUTTON ();

INITFIELD ();

THIS.SETSIZE (300, 200);

// this.pack ();

}

Private void setLayout () {

THIS.GETCONTENTPANE (). setLayout (New flowLayout ());

}

private vidin {

Namelist = new jlist (getNames ());

JscrollPane scroll = new jscrollpane (namelist);

THIS.GETCONTENTPANE (). Add (scroll);

}

Private vidin () {

AddButton = New JButton ("add");

AddButton.AddActionListener (New ActionListener () {Public Void ActionPerformed (ActionEvent E) {

myEditor.Add (Namefield.getText ());

Movielist.setListData (GetNames ());

}

});

THIS.GETCONTENTPANE (). Add (address);

}

Private voidiniLD () {

Moviefield = New JtextField (13);

THIS.GETCONTENTPANE (). Add (namefield);

}

PRIVATE Vector GetNames () {

Vector names = myEditor.getNames ();

Return (names == null)? new vector (): name;

}

}

Ok, now everything is ready, we can write to test, as follows:

Package testgui;

Import junit.framework. *;

Import org.easymock. *;

Import javax.swing. *;

Public Class TestBasegui

Extends testcase {

Private MockControl Control;

Private listeditor mockeditor;

Private Listwindow Window;

Protected void setup () throws exception {

Super.setup ();

Control = MockControl.createniceControl (listeditor.class);

MockEditor = (listeditor) control.getmock ();

Control.Replay ();

Window = New ListWindow (Mockeditor);

WINDOW.INIT ();

WINDOW.SHOW ();

}

Public void testlist () {

Jlist namelist = window.getnamelist ();

AssertNotnull ("List Not Null", Namelist;

Asserttrue ("is showing", namelist.isshowing ());

}

Public void testAddbutton ()

{

JButton AddButton = WINDOW.GETDBUTTON ();

Assertequals ("Should Be Add", "Add", AddButton.getText ());

}

Public void testfield ()

{

Jtextfield namefield = window.getnamefield ();

AssertNotnull ("Text Not Null", Namefield;

}

protected void teardown () throws exception {

Super.teardown ();

}

}

This test is very simple, it is just whether the components on the Test the GUI really exist (non-empty). It didn't mean it, but it is so simple that it is for more concentrated demonstration of Easymock. In the above code, we first got a MockControl in the MockControl's static factories in Easymock, and Nice, which allows you to care about its return value), then use Control We get a MOCK and will Forced to convert To listitor, then we can use it (it is best to activate it with a replay before use). It's so simple. If this test passes, then your GUI has no problem. When you use it in the project, you only need to implement a class, let it imports, let it send it to GUI. . The following test is more complex, it tested the function of each component in the GUI, but the principle is the same:

Package testgui;

Import junit.framework. *;

Import org.easymock. *;

Import java.util. *;

Import javax.swing. *;

Public Class TestmovielistWindow

Extends testcase {

Private ListWindow ListWindow = NULL;

Private static final string addstring = "Test a mock!";

Private vector name = NULL;

Private MockControl Control = NULL;

Private listeditor mockditor = null;

Protected void setup () throws exception {

Super.setup ();

/ ** @ Todo Verify the constructors * /

Name = new vector () {

{

Add ("Hello");

Add ("HIT");

}

}

Control = MockControl.createControl (listeditor.class);

MockEditor = (listeditor) control.getmock ();

}

Public void testlist () {

Mockeditor.getNames ();

Control.setReturnValue (Name, 1);

Control.Replay ();

Window = New ListWindow (Mockeditor);

WINDOW.INIT ();

WINDOW.SHOW ();

Jlist namelist = window.getnamelist ();

Listmodel namelistmodel = namelist.getmodel ();

Assertequals ("Wrong Size", Name.Size (), NameListModel.getsize ());

Assertequals ("Should Be Hello!", "Hello", NamelistModel.getElementat (0));

Control.verify ();

}

Public void testAdd () {

Vector namewithaddition = new vector (name);

NamewITHADDITION.ADD (AddString); mockditor.getnames ();

Control.setReturnValue (Name, 1);

MockEditor.Add (addstring);

Control.setvoidCallable (1);

Mockeditor.getNames ();

Control.setReturnValue (Namewithaddition, 1);

Control.Replay ();

Window = New ListWindow (Mockeditor);

WINDOW.INIT ();

WINDOW.SHOW ();

Jtextfield namefield = window.getnamefield ();

Namefield.Settext (addstring);

JButton AddButton = WINDOW.GETDBUTTON ();

AddButton.doclick ();

Jlist namelist = window.getnamelist ();

Listmodel namelistmodel = namelist.getmodel ();

Assertequals ("Wrong Size", namewithaddition.size (),

NameListModel.getsize ());

Assertequals ("Should Be Test A Mock!", AddString,

NamelistModel.getylementat (2));

Control.verify ();

}

protected void teardown () throws exception {

Super.teardown ();

}

}

In this test, the Control we returned is not Nice, so you have to indicate the number of returns and calls in each time, and the rest is not changed.

For more information, see the documentation it provides.

This time, we use Easymock with Easymock with Easymock with Easymock. Next, I will demonstrate with a better way to test, and will introduce the concept of "super thin" GUI!

(Code is passed in jbuilder2005 xp sp1)

转载请注明原文地址:https://www.9cbs.com/read-39384.html

New Post(0)