Recent projects have unit tests in writing Java code, and then in thinking about a problem, why write unit tests?? What's the use of unit tests?? Baidu has a lap, as follows:
- Software quality is the simplest and most effective guarantee;
- Is the clearest and most effective document of the target code;
- Can optimize the design of the target code;
- is the protection of code refactoring;
- is the cornerstone of regression testing and continuous integration.
Because the development experience is limited, may say is not quite right, but is my current personal view, writes unit test, sometimes actually can find the bug, but found that the number of bugs is very few, and at present is the project development is finished, to go online, the company has 80% coverage requirements, so all are late on-line before the completion. For now, I'm not writing ut very seriously, just thinking about completing the on-line request. This thing, but also look at the cost requirements, if a new project to emergency on-line, take the emergency release of special procedures, unit testing later time enough to fill the line. So, in the case of time permitting, I think I still have to write UT, do sometimes do find some problems, especially for a large project, the longer a bug is hidden, the greater the cost of fixing the bug. A large number of research data have been quoted in the book "Rapid Software Development" to point out that the cost of modifying a bug at the end is 10 times times the cost of modifying it when the bug is generated . In addition, you can learn some unit testing knowledge, but also a kind of skill progress.
The Java Unit Test tool Junit4+mock (Mockito/jmock/powermock) or Stub (which is less used and generally not recommended)is now widely used. because junit3 currently used not much, the basic upgrade to JUNIT4, so it is straightforward to say JUNIT4.
Question one: Why do I need a mock or stub? What does it have to do with JUnit?
When we do unit testing, we will find that the method we are testing will refer to many externally dependent objects, such as: (Send mail, network communication, log log, file system, etc.). And we can't control these externally dependent objects. To solve this problem, we need to use stubs and mocks to simulate these externally dependent objects to control them.
JUnit is a unit test framework that makes it easy to complete unit testing of a class that has fewer dependencies or simpler, but it can be time consuming to simulate the environment or configure the environment for unit tests that are associated with other complex classes or classes that require a running environment, and it is difficult to implement unit tests. And these "mock frames"(Mockito, Jmock, Powermock, EasyMock) can simulate the behavior of an object through a mock frame, isolating other objects that we don't care about, making testing simple. ( For example, service calls DAO, Service relies on DAO, we can mock DAO to simulate real DAO invocation, so we can achieve the purpose of testing service.) )
Mock object can replace the location of real objects, to test some of the functions that interact with real objects or rely on real objects, and the purpose behind the mock objects is to create a lightweight, controllable object that replaces the real objects needed in the test, simulating the behavior and functions of real objects.
Question two: What is the difference between a mock and a stub?
Mock and stub are two ways to test code functionality. The mock weight is applied to the simulation of the function, and the stub is measured to reproduce the function test. For example, for the list interface, the mock simulates the list directly, and the stub creates a new testlist that implements the list, in which the code for the test is written.
It is highly recommended that you choose the mock mode First, because the mock-up code is put together with the test code, is easy to read, and has better extensibility and flexibility than the stub.
where Easymock and Mockito emulate the way that the Java interface uses interface proxies, the Java class uses inheritance to simulate (and a new class class is created). Mockito supports spy mode and can simulate instances. But none of them can simulate static and final classes, and Powermock supports this functionality by modifying the bytecode.
An article about: http://blog.csdn.net/devhubs/article/details/8018084
Second, JUNIT4 related introduction
here is an article about some of the JUNIT4, including how to introduce, use, pretty detailed. ---" http://blog.csdn.net/happylee6688/article/details/38069761
Here is a few common notes, as learning convenience.
Common Annotations
@Before: The initialization method, the code that must be executed before any test method executes . Compared to JUnit 3, the SetUp () method has the same functionality. In this annotated method, you can do some preparatory work, such as initializing the object, opening the network connection, and so on.
@After: Frees up resources that need to be finalized after any test method has been executed. Compared with JUnit 3, the TearDown () method has the same functionality. @Test: The test method indicates that this is a test method. will be executed automatically in JUnit. The declaration of a method also has the following requirements: The name can be arbitrarily taken without any restrictions, but the return value must be void and cannot have any arguments. If these rules are violated, an exception is thrown at run time. However, in order to develop a good programming habit, we usually add test to the method name tested, for example: Testadd ().
At the same time, the Annotation (@Test) can also test the expected exception and timeout time, such as @Test (timeout=100), we give the test function to set an execution time, over this time (100 milliseconds), they will be forcibly terminated by the system, And the system will tell you that the function ends because of a timeout, so you can spot the bugs. Moreover, it can also test the expected exception, for example, we just had a null pointer exception: @Test (Expected=nullpointerexception.class).
@Ignore: Ignored test methods, the meaning of labeling is "some methods have not been completed, we do not participate in this Test", so the test results will indicate that you have a few tests are ignored, not failure. Once you have completed the corresponding function, just delete the @Ignore annotation and you can test it properly. Of course, this @Ignore annotation is meaningful for people like me who have obsessive-compulsive disorder. Every time you see a red bar (the test fails) you get sick and feel unbearable (unless the goal of testing is to make it fail). Of course, the same is true for code, which cannot tolerate the clutter of code.
@BeforeClass: For all tests, that is, the entire test class, the method that is annotated is executed first, and only once , before all test methods are executed . Of course, it should be noted that the modifier must be public static void xxxx; This Annotation is a new feature of JUnit 4.
@AfterClass: For all tests, that is, the entire test class, the method that is annotated is executed only once after all the test methods have been executed . Of course, it should be noted that the modifier must also be public static void xxxx; This Annotation is also a new feature of JUnit 4, which is a pair with @BeforeClass.
Execution Ordertherefore, in JUnit 4, the order in which the unit test cases are executed is:
Iii. Several comparisons of mock (Mockito, Jmock, Powermock)
Article One: http://blog.csdn.net/luvinahlc/article/details/10442743
Original! Some summary of Java Unit Testing