Use Easymock in unit test

xiaoxiao2021-04-03  227

We must write a large number of unit tests in our daily development, many unit tests are relatively independent, such as a module for incrementary tax calculations, but more unit testing requires other components or services. Therefore, we need Easymock to seamlessly connect these components. Mock's means is empty, there is something that does not exist.

Easymock has just released version 2.2, which starts using many JDK 1.5-based features.

Let's take an example:

Assume that the following interface is defined:

Public interface isimpsonservice {iepisode getepisode (int number);}

Public interface iepisode {int getNumber (); string gettitle (); inputstream getDataAsStream ();

The implementation of this interface is

Public Class ClientsImpsonService Implements IsimpsonService {

Private isimpsonservice remote.

Public ClientSimpsonservice (Isimpsonservice RemotesImpsonService) {

This.RemotesimpsonService = RemotesImpsonService;

}

Public IEPisode getEpisode (int adjgisodenumber) {

Return NULL;

}

}

The unit test of the service is as follows

Public Class ClientsimpsonserviceTest Extends Testcase {

Public void testclientsImpsonservice () {

Try {

New Clientsimpsonservice (NULL);

Fail ("EXPECTED ILLEGALARGUMENTEXCEPTION);

} catch (IllegalargumentException E) {

// expected

}

}

We can see because there is no RemotesImpsonService, so we only pass a null to ClientsImpsonService.

This is unable to complete the test. So we have replaced the REMOTESIMPSONSERVICE that has not been written.

Import static org.easymock.easynmock.createmock;

Import junit.framework.testcase;

Public Class ClientsimpsonserviceTest Extends Testcase {

Private isimpsonservice remoteSimpsonservicemock;

Protected void setup () throws exception {

Super.setup ();

RemoteSimpsonServiceMock =

Createmock (isimpsonservice.class);

}

Public void testclientsImpsonservice () {

Try {

New Clientsimpsonservice (NULL);

Fail ("EXPECTED ILLEGALARGUMENTEXCEPTION);

} catch (IllegalargumentException E) {

// expected

}

New Clientsimpsonservice (RemotesImpsonServiceMock);

}

}

In the above black body code, we created an ISIMPSONSERVICE.CLASS MOCK, which has the same interface as ISIMPSOnService.

The same method, we can define object IEPisode returned by Isimpsonservice

Import static org.easymock.easynmock.createmock;

Import static org.easymock.easymock.expect;

Import static org.easymock.easynmock.replay;

Import static org.easymock.easymock.Verify;

Import junit.framework.testcase;

Public Class ClientsimpsonserviceTest Extends Testcase {

Private iepisode episode17mock;

Private isimpsonservice remoteSimpsonservicemock;

Protected void setup () throws exception {

Super.setup ();

Episode17mock = Createmock (Iepisode.class);

RemoteSimpsonServiceMock =

Createmock (isimpsonservice.class);

}

...

Public void testgetepisode () throws exception {

EXPECT (RemoteSimpsonserviceMock.Getepisode (17))

.andreturn (EPISODE17MOCK);

Replay (RemoteSimpsonservicemock);

ISImpsonservice ClientsImpsonService =

New Clientsimpsonservice (RemotesImpsonServiceMock);

IEPISode Result = ClientsImpsonService.Getepisode (17);

Verify (RemotesImpsonServiceMock);

Assertequals (EPISODE17MOCK, RESULT);

}

The above is just a throwing brick, I want to know more Easymock, please see

http://today.java.net/pub/a/today/2006/06/20/getting-started-with-easymock-2.html

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

New Post(0)