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)