Use JUnit to test Dao in SSH

Source: Internet
Author: User
JUnit testing SSH JUnit can be used to test Java Web applications that use the SSH open-source framework. The DAO method and service-layer business logic method can be easily tested without relying on the framework.

To test the methods in Dao and service, you must first obtain the class instance. I think this is the most important thing. In SSH, Dao and service are both managed through spring, so they cannot get corresponding instances through new.

Use the following method to obtain Dao and service instances.
Public class xxxdaotest {private beanfactory; private xxxdao; @ beforepublic void getdao () {// you need to modify it as needed and find the configuration file in the classes path. Beanfactory = new classpathxmlapplicationcontext ("applicationcontext. XML "); xxxdao = (xxxdao) beanfactory. getbean ("xxxdao") ;}@ test public void testxxx () {// method of test. }

Of course, to use JUnit, you must add the corresponding JUnit and its dependent jar packages, which can be easily found on the Internet. If you don't want to use it yourself, many ides also provide support. For example, if you use myecplise, you can find the JUnit toolkit in build path-add libraries, and click it to select a version.

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.