????????

??????Linux????????????????У????????????????????????socket??????????????????????????????????????????????????????в?????????????????????????類???????鲿?????????????????????????????????????????????????????Щ?????????????????????????

?????????????????У????????????????д?Щ???????????????????????????????д???????????????????е?????????С???????????Э?鯔?????????????????????ù??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

????????MOCK

????Mock???????????????????????????

????Mock?????

????“Mock Objects simulate parts of the behavior of domain code?? and are able to check whether they are used as defined. Domain classes can be tested in isolation by simulating their collaborators with Mock Objects.”???????λ????EasyMock???????????????????CASE??????????Щ??????????Mock Object???????Щ???

????Mock?????

?????????????У?????????????Mock????????????????????????????????????????????????Ч?????????Щ????Mock????????????? “EasyMock”??“GMock”???

????“Hand-writing classes for Mock Objects is not needed.

????Supports refactoring-safe Mock Objects: test code will not break at runtime when renaming methods or reordering method parameters

????Supports return values and exceptions.

????Supports checking the order of method calls?? for one or more Mock Objects. ”

??????????EasyMock?????????Mock??????????????????????????????????????????????????????????????????????????

????????EasyMock??????????????ο? EasyMock????????

????Mock??????е????

???????Mock???е??????????????????????(???EasyMock??????)

????Define Interface

????public interface Collaborator { void documentAdded(String title); void documentChanged(String title); void documentRemoved(String title); byte voteForRemoval(String title); byte[] voteForRemovals(String[] title); }

????Define a Model Class

????public class ClassUnderTest { // … public void addListener(Collaborator listener) { // … } public void addDocument(String title?? byte[] document) { // … } public boolean removeDocument(String title) { // … } public boolean removeDocuments(String[] titles) { // … } }

????Create a Mock Object

????protected void setUp() { mock = createMock(Collaborator.class); // 1 classUnderTest = new ClassUnderTest(); classUnderTest.addListener(mock);} public void testRemoveNonExistingDocument() { // 2 (we do not expect anything) replay(mock); // 3 classUnderTest.removeDocument(“Does not exist“);}

????Adding Behavior

????public void testAddDocument() { mock.documentAdded(“New Document“); // 2 replay(mock); // 3 classUnderTest.addDocument(“New Document“?? new byte[0]); }

????Specifying Return Values

????public void testVoteForRemoval() { mock.documentAdded(“Document“); // expect document addition // expect to be asked to vote for document removal?? and vote for it expect(mock.voteForRemoval(“Document“)).andReturn((byte) 42); mock.documentRemoved(“Document“); // expect document removal replay(mock); classUnderTest.addDocument(“Document“?? new byte[0]); assertTrue(classUnderTest.removeDocument(“Document“)); verify(mock);}

????????Mock Server

????????????????Mock???????????Mock Object????????Mock Server?????????????????????????????????????????????????Stub Server??

????MockServer????????

??????????????

??????????????????????????Stub Server????????

????Stub Server??????????????????????????????????????????棬????????????????????????????????????????????????????????Щ?????????????????Щ?????Ρ?

????????????????Stub Server?????????????

 

????????SOCKET?????????????WHILE NOT STOP: ??socket?ж????? IF ????????[????] THEN ????[???????] END IFEND WHILE

???????????????????????Stub Server???????????????????Э?飬??????????н????????????????????????????????????????????????????????? ???????????????Щ??????????????????????????????????????????????????????

????????????????????????????????????????????????????????????????滻???е?[????]??[???????]????????????CASE???????????? [????]??[???????]???????????????????????????????????????????????????????????????????????[????]??[???????]??????????в????????????????????????????????????????????????????????????????????????????????????????????????????????飬???????????????????Щ???????????????и?????????????????????????????顣

????MockServer???

????MockServer??????????????????????????????????????????????????????????????????????[????]??[???????]?????????????????????????????????????????????Э?飬????????????????????mock??????????????????????????

???????????????socket??Mock Server????????????

????????SOCKET?????????????WHILE NOT STOP: ??socket?ж????? ???[MOCK???] END IFEND WHILE

????????MOCK????????????????????

????ON: recieve(‘HELLO‘)DO: sendback(‘WORLD‘) keep_alive()ON: recieve(‘QUIT‘)DO: close_link()

?????????Mock Server????????????????[MOCK???]