Two little considerations for Java unit test

xiaoxiao2021-04-07  377

Introduce developers write unit tests to check their code. Unit tests are different from integrated testing and acceptance tests. Integration tests ensure collaborative work of each component, the acceptance test guarantee program to meet the customer expectations. The unit test is only tested for one unit of the code. In Java, a unit usually refers to a class. The unit test requires automatic, no manual interaction, and non-success. So, when you run the code, check if his output is correct, this process is not a unit test. Unit test requires some tests to drive your code, and you can view the results of the work. All along, unit tests have been staying in their mouths, but now it has finally become a professional tool for Java developers. It is not enough Java programmers that become a master level. Now you have to know how to test your code correctly, so you can lead you high quality code and have high efficiency, low maintenance costs. In this chapter, the two points are mainly discussed. When the Java programmer makes the unit test, it has been plagued by this two points. Do you have to design a system in a convenient way to test the uncertainty code.

Don't sacrifice the design unit testing and it to test the relationship between the design unit test and it to test the system is a very common problem. So first discuss this. When you write a unit test, sometimes it is forced to change the code to accommodate the test. Especially when you want to test a private method or attribute. If you change the private method to the public method is purely for the purpose of the test, don't do this. Test means lifting the quality of the code, not a decrease. This is said that sometimes it is necessary to make it easy to test when designing the system. If you want to join a design support test, you must ensure that it will enhance the overall quality of the system. If this is not the case, you just designed for testing, then I have to emphasize, this is very bad. For example, you have a system that connects the database. If your system is connected to test the database and is the same convenience as the development database, then this is fine. This makes it easy to test if you design it to allow the database. At the same time, you have also strengthened the quality of your design, because your system is more flexible (you can connect to the product database without changing the code). This design benefits the system and test, so it is a good decision. Your class can only be instantiated by factory methods. You need to test a class object, but for some reason you can't call the factory. It may require some resources as parameters, but there is no in your test environment. So whether you can change the default constructor of this class to public, so you can instantiate the class in the test. This reduces the quality of the system design, allowing anyone to access this constructor. In this case, the programmer needs to provide the necessary resources, instantiate the class from the factory (can be used with Mock Object). Now, we will discuss this problem, we check some solutions for common test problems. The first question is how to test the uncertainty of uncertainty.

The code with statistical tests for uncertainty is uncertain. That is, the factors affecting a specific result of a method are not limited to the code within this method. The return value of System.CurrentTimeMillis () is the simplest example. The exact result is not depends on the code but the bottom layer hardware. The value returned is based on the system clock, and each time the code is running, there will be different return results. Another example is the time consumption of the test message from the server to the browser. To build a model that can be predicted requires too much variable. At the fine grain level, the network is uncertain or even confusion. However, unpredictable at fine grain level does not indicate that comprehensive behavior is unpredictable. If you collect a lot of fine particle results, you can get effective forecasting for expectations. For example, your needs are 90% of network services to be completed at 1/100 seconds. A simple way is that you can repeatedly run the code, save the number of pass and failures. If 90% fail, the test solution failed. The following code to ensure that the performance of the service within a reasonable range: private int doPerformanceTest (int numberOfRuns, int requiredTimeInMilliseconds) {int passed = 0; Fragment fragment = new Fragment (); for (int i = 0; i

Using this method of testing should assertion responsible for verifying: public void testPerformanceStatistics () {double percentageRequired = 90.0; int numberOfRuns = 1000; int passed = doPerformanceTest (numberOfRuns, 100); double percentagePassed = passed / numberOfRuns; assertTrue ( "percentagePassed = " PercentagePassed, PercentagePassed> = PercentageRequired);} This statable test technology is useful for locating a lot of problems. In particular, those results are tested based on the date, time, or random number. The non-deterministic attribute of test performance and code is also very valuable.

Unit testing is important for programmers. There are a lot of tools in Java to help development. These tools are needed in your project. The write unit test is quite easy and is very effective. It will not cost too much time to run.

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

New Post(0)