JUnit Basic Learning-testing the service layer (3)

Source: Internet
Author: User

Test steps:

The JUNIT environment has been added to the previous article, which is not required.

1. Load the JUnit class, spring configuration file, indicating the JUnit tester, @Runwith

2. Define variables, service, cannot use spring annotations, because spring annotations are built on the server.

3. Initialize the @before annotations.

4. Implement test methods, @Test annotations.

1  PackageSwust.edu.cn.postdoctors.service.impl;2 3 ImportJavax.annotation.Resource;4 5 Importorg.junit.Test;6 ImportOrg.junit.runner.RunWith;7 Importorg.springframework.beans.factory.annotation.Autowired;8 Importorg.springframework.test.context.ContextConfiguration;9 ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;Ten  One ImportSwust.edu.cn.postdoctors.service.UserService; A Importjunit.framework.TestCase; -  -@RunWith (Springjunit4classrunner.class)//Integration the@ContextConfiguration (locations={"Classpath:spring-mybatis-test.xml"})//Load Configuration -  Public classUserservicetestextendsTestCase { -  -  +     PrivateUserService UserService; -      +      PublicUserService Getuserservice () { A         returnUserService; at     } -  -  -      Public voidSetuserservice (UserService userservice) { -          This. UserService =UserService; -     } in  -Omit Setget
to @Test + Public voidTESTSELECTUSERBYLOGINNAMEANDPSWD ()throwsException { - theSwust.edu.cn.postdoctors.model.User Resuser =NULL ; * $Resuser = userservice.finduserbyloginnameandpswd ("smx", "123");Panax Notoginseng if(Resuser = =NULL){ -SYSTEM.OUT.PRINTLN ("UserService Error! "); the}Else{ +System.out.println ("UserService right! "); A } the + } -} $ -

JUnit Basic Learning-testing the service layer (3)

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.