Unit testing is the basis of the integration test and system test and foundation of test-driven development and software refactoring. And as test-driven development grows in popularity, unit testing is increasingly important. This paper mainly studies the Java program unit testing. Based on the research of the unit test theory and tools, found that the current Java program unit testing also has some problems. These problems include: the current test framework can’t choose the test cases automatically, also cannot automatically add test cases to Test Runner; When you need to frequently and selectively running a large number of tests, the manual test is very tedious; The mock framework used in unit testing has the limitations such as can not simulate the final class. In the test method, mixing the mock code with the test code not only affects the readability of test methods, but is also not conducive to the reuse of mock object; The current test framework don’t provide effective support for the reuse of test methods.To solve above problems, combining unit tests and aspect-oriented programming, we research the application of AspectJ(an aspect-oriented framework) in Java unit test. The main results are as follows.1) First of all, scan the entire program and automatically add test cases to test suite, and then the framework automatically run the test cases in test the suites. In the process of adding test cases, Using AspectJ’s crosscutting function crosscutts the method that used to add test cases, make the framework only to only add test cases described in the aspect. Realize the function that the test framework can automatically select, add and run the test case.2) Capture all dependencies on external resources of the tested method using AspectJ’s pointcut. In the asepct, we rewrite the depended method to isolate dependences. And design the aspect model for isolating dependences. We can set expected methods’ invocation in aspect. Then, capture the actual method’s invocation using the pointcut when the tested method is running. Then verify the consistent of actual invocations and expected invocations. We realize the interactive test, and design interactive test’s aspect model.3) Two methods of reusing test method is designed. They are the parametric test and the reuse of interface test method. Capture the invocation of tested method in the test method using AspectJ’s pointcut. We use multiple sets of test data to loop calling the tested method, and record the result of A single set of test data. Capture the invocation of assert methods in the test method. Reuse assert methods to assert the result for A single set of test data. Finally, realize parametric tests. We use AspectJ’s static crosscutting function to let each implementation class’ s test class reuse test methods of interface’s methods implemented in the aspect. Finally, realize the reuse of interface test method.In order to facilitate the use of designed functions in the actual unit tests, this paper designs and realizes Test WAJ(Test with AspectJ) based on JUnit. Finally, unit test with Test WAJ, the results show that the proposed solutions are really feasible. This paper realized the function that the test framework can automatically select, add and run the test case. It improve the level of automation of Java unit test. Because of designing two methods of reusing test method, it can enhance the test method’s reusability and reduce the test cost. The mock framework implemented by AspectJ is more stronger, and has higher reusability and flexibility than the traditional mock framework. |