Mock Object tool lateral comparison (below)

zhaozj2021-02-17  99

Easymock

With Easymock's own words: Easymock is a class library trows an easy war given interfaces. Easymock provides a rich Mock Object. You can check the order of the method call, you can set the range of a particular method call, and you can even do not check the consistency of the Mock Object method. It can be said that the most flexible one is used in Easymock's dynamic MOCK tools.

Here, let's take a look at its test method:

Public void testsimpleadditionusingesymock () throws exception {

2. MockControl Control1 = MockControl.createControl (httpservletRequest.class);

3. httpservletRequest MockhttpServletRequest =

4. (httpservletRequest) Control1.getmock ();

5. MockControl Control2 = MockControl.createstrictStrictControl (httpservletResponse.class);

6. httpservletResponse mockhttpservletResponse =

7. (httpservletResponse) Control2.getmock ();

8.

9. MockhtpservletRequest.getParameter ("B");

10. Control1.setReturnValue ("4", 1);

11. MockHttpServletRequest.getParameter ("a");

12. Control1.setReturnValue ("3", 1);

13.

14. Final StringWriter Output = new stringwriter ();

15. Final PrintWriter ContentWriter = New PrintWriter (OUTPUT);

16.

17. MockhtpServletResponse.setContentType (Text / HTML ");

18. Control2.setvoidCallable (1);

19. mockhtpservletResponse.getwriter ();

20. Control2.setReturnValue (ContentWriter, 1);

twenty one.

22. Control1.Replay ();

23. Control2.Replay ();

twenty four.

25. SimpleCalcServlet ASERVLET = New SimpleCalcServlet ();

26. ASERVLET.DOGET (MockhtpServletRequest, MockhttpservletResponse);

27.

28. Control1.Verify ();

29. Control2.Verify ();

30. Assertequals ("Output Should Be An Addition", "Result = 7", Output.toString ()); 31.

It can be seen that the code style is exactly the same as MockObjects, but it is only slightly different.

When LINE2 is created with Line5, when Line2 creates a Controller that does not check the order, line5 creates a Controller that checks the order. Then call the Controller's getMock () get a MOCK instance.

LINE9 is directly called for MockhttpservletRequest, and the method to be called to be called directly; then the line 10 sets the number of return values ​​and calls to call this method. Finally, line22, line23 calls Controller's replay () method, indicating that the training is over, can start testing.

Mockcreator

MockObjects and Easymock can only create a Mock Object instance for interfaces (Interface), which has been a good habit of using the interface, although there is nothing to complain. However, the world is not satisfactory, and when we have to write a Mock implementation for some Class, at this time, the tools that can automatically generate the Mock Object implementation of MockCreator will be used.

MockCreator can generate the Mock Object Class in the form of command lines, or the officially provided plugin is convenient in Eclipse. The following figure is the method of operating the Mock Object Class using the MockCreator plugin in Eclipse:

For the example shown above, the class named MockhttpServletRequest is named MockHttpservletRequest, which is placed under the same package with httpservletRequest.

How is it, it is very simple to generate a Mock Object implementation, isn't it? J

Let's take a look at the test method you write:

Public void testsimpleadditionusingmockcreator () throws exception {

2. Final MockhtpServletRequest MockHttpServletRequest = New MockHttpServletRequest ();

3. Final MockhttpServletResponse MockhttpServletResponse =

4. New mockhttpservletResponse ();

5.

6. mockhtpservletRequest.StartBlock ();

7. MockhtpservletRequest.expectGetParameter ("B", "4");

8. MockhtpServletRequest.ExpectGetParameter ("a", "3");

9. mockhttpservletRequest.endblock ();

10.

11. Final StringWriter Output = new stringwriter ();

12. Final PrintWriter ContentWriter = New PrintWriter (OUTPUT);

13.

14. MockhtpServletResponse.expectSetContentType ("text / html"); 15. MockhttpservletResponse.expectGetwriter (ContentWriter);

16.

17. SimpleCalcServlet ASERVLET = New SimpleCalcServlet ();

18. ASERVLET.DOGET (MockHttpservletRequest, MockHttpservletResponse);

19.

20. mockhtpservletRequest.verify ();

21. mockhtpservletResponse.verify ();

22. Assertequals ("Output Should Be An Addition", "Result = 7", Output.toString ());

twenty three. }

Note that in line6, line9 calls the startBlock () and endblock () methods, indicating that when the final validation, does not check the order in the block within the block. Line7, line8 indicates that the measured code will call the getParameter () method in parameter "b", "A", and MockhttpservletRequest will return "4", "3".

to sum up

As can be seen from the code above, the model using these three tools is exactly the same, the function is basically the same, only the user interface they provide is slightly different.

Although Easymock is slightly complicated, it is also the most flexible use, and there is a uniqueness when testing more recursive changes.

MockObjects and MockCreator interface are easy to use. But one is a dynamic MOCK, one is a static Mock, which can be determined based on the actual situation.

In addition, in the case where Mock Object is required to establish Mock Object for Class, only MockCreator can be held.

The most important thing, according to the actual needs of the project, we can also use multiple tools to help us test to meet different needs.

Reference

1, www.mockobjects.com, hometown of Mock Object Project, is committed to establishing a Mock Object Standard Framework.

2, www.easynmock.org, Easymock, is committed to providing a simple way to help create Mock Object.

3, MockCreator.sf.net, MockCreator, automatically generate Mock Object, providing easy-to-use Eclipse plugins.

4, www.mockmaker.org, Mockmaker, similar to Mock Creator Similar Mock Object Automatic Generation Tools. Since the implementation of the Mock Object implementation is not correct in some environments, it is excluded.

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

New Post(0)