Analog objects (-)

xiaoxiao2021-03-06  23

I have been busy with my blog recently. Take a little time these two days, write something tested! The general thing is very much online, I will talk about the role of simulation objects in unit test!

TDD is now being accepted by most people, even if you don't have this development method, at least you will use unit test.

We know that the basic requirements for unit tests are to ensure that the environment of each test is independent, and the speed of the test is required to be tried to quickly. It is generally very easy to satisfy, but in some cases, some contradictions appear: When there is a random number in our program, when we have to test the database, when we need to test the GUI ... then What should we do at this time? Using an analog object (Mock). It can help us produce controllable and lightweight objects.

Of course, you can realize analog objects yourself, but when you write a few such procedures, you will feel Sori, why? Because their basic structure is the same. The help of the present invention also found this, so they finally wrote a framework (MockObject), inherited it, you can only pay attention to actual business logic without having to spend on those stunned frameworks. But the simulation object that is still needed, and later, the part of the people will make a mockmaker, and I will only provide an interface or class, it can automatically generate it. The simulation object you need, and its operation has a swing interface, it is very a provincial thing ^ _ ^!

Maybe you think this is fine, but there is a problem: we need extra more manages several classes in your code! Maybe you feel that there is nothing, but when your program is large, it is likely that the synchronization problem will appear after modifying some of the code. then what should we do? Sacrifice partial flexibility, using dynamic generated methods - EASYMOCK.

Easymock's idea is very simple, I don't have to generate a class, as long as you give me an interface, I use this interface to dynamically generate an analog object with this interface in memory, so that you don't increase your needs and add you. Attached burden of additional management! Of course, I have already said in front, this is the price (except for some effects of performance): EasyMock generated analog objects are passive, it can only receive data and verify! Of course, this is not a problem in most cases!

Ok, about this basic thing about the simulation object, the next article I will rely with Easymock to demonstrate how to test the GUI!

(These three frames mentioned above) You can go to SourceForge to download)

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

New Post(0)