unittest--4.15 Update of business layer under spring

Source: Internet
Author: User
Tags string
After a long time, finally began to use UnitTest. Cause one, like everyone else, doesn't want to have nightmares at night, because the second is that the business layer TDD in spring is able to get rid of the need for Tomcat, completely away from the development progress of the display layer, and focus on the rapid development of the business layer.
But we also use unittest only at the business level, because the display layer in MVC has so far no good unittest method (whether it's a httpunit or a clunky GUI test tool), and our business logic is tightly encapsulated in the business layer, The controler layer only makes the basic action of assembling and dispatching, so it is not necessary to take great effort to test.

The tests under spring are simple, and even though the handwriting applicationcontext is simple to load and bean creation, Spring provides greater convenience in two ways

Dynamic injection of 1.bean

Originally own manual load also not trouble, but as long as your testcase inherit Spring-mock.jar in Abstractdependencyinjectionspringcontexttests, You can even declare a variable as protected, and you get an automatic injection.

2. Maintenance of the database environment

Spring's solution is also very simple, he will automatically for each method, mandatory rollback, so there is no cleanup-maintenance problems, as long as your testcase inherited from Abstracttransactionaldatasourcespringcontexttests.

At the same time, this abstracttransactionaldatasourcespringcontexttests has the function of abstractdependencyinjectionspringcontexttests above.

3. Further simplification

First of all, the names of the two base classes are too long, and there are some common settings, such as executing setpopulateprotectedvariables (true) in the constructor, as long as the declaration protected is dynamically injected, Otherwise, the setter is also written to be injected dynamically. For example, the definition of some public context files.

So we abstracted a base class public class Daotestcase extends abstracttransactionaldatasourcespringcontexttests{protected ArrayList <String> contexts = null;

Public Daotestcase () {//Set this, can autowire by name, otherwise by setter. Setpopulateprotectedvariables (TRUE);

contexts = new arraylist<string> ();  Contexts.add ("/applicationcontext-hibernate.xml"); Contexts.add ("/applicationcontext-setupservice.xml"); }

Public string[] Getconfiglocations () {string[] tmp = new string[contexts.size ()]; return Contexts.toarray (TMP); }}

The actual subclass public class Customerdaotest extends daotestcase{protected Customerdao Customerdao;

public void Testgetcustomer () throws Exception {Customer customer = Customerdao.lookcustomer (1); Assertequals ((i NT) Customer.getcustomerno (), 1)}}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.