Http://www.51testing.com/html/14/n-1408814.html
1. Test the bean injected directly in spring (in DAO, for example):
Adding @runwith annotations on a test class specifies that the test runner using springjunit, @ContextConfiguration annotations Specify the location of the spring configuration file for the test, we can inject the bean we need to test, JUnit parses the spring configuration file before running the test, initializing the bean configured in spring
1@RunWith (Springjunit4classrunner.class)2 3@ContextConfiguration (locations={"Classpath*:spring-config-test.xml"})4 5 Public classTestprojectdao {6 7 @Autowired8 9 Projectdao Projectdao;Ten One @Test A - Public voidTestcreateprojectcode () { - the LongApplytime =System.currenttimemillis (); - -Timestamp ts =NewTimestamp (applytime); - +Map CodeMap = Projectdao.generatecode ("5", "8", TS, "in hospital")); - +String projectcode = (string) codemap.get ("_project_code"); A atTimestamp apply_time = (Timestamp) codemap.get ("_apply_time"); - - System.out.print (projectcode); - - System.out.print (apply_time.tostring ()); - inAssert.asserttrue (Projectcode.length () ==12); - to}
View Code
2. Test the SPRINGMVC:The ORG.SPRINGFRAMEWORK.TEST.WEB.SERVLET.MOCKMVC class appears after spring3.2, and the sample support for the SPRINGMVC unit test is as follows:
1 PackageCom.jiaoyiping.baseproject;2 3 ImportCom.jiaoyiping.baseproject.privilege.controller.MeunController;4 5 ImportCom.jiaoyiping.baseproject.training.bean.Person;6 7 ImportJunit.framework.Assert;8 9 ImportOrg.junit.Before;Ten One Importorg.junit.Test; A - ImportOrg.junit.runner.RunWith; - the Importorg.springframework.beans.factory.annotation.Autowired; - - ImportOrg.springframework.http.MediaType; - + Importorg.springframework.test.context.ContextConfiguration; - + ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner; A at Importorg.springframework.test.context.web.WebAppConfiguration; - - ImportORG.SPRINGFRAMEWORK.TEST.WEB.SERVLET.MOCKMVC; - - Importorg.springframework.test.web.servlet.ResultActions; - in Importorg.springframework.test.web.servlet.setup.MockMvcBuilders; - to ImportOrg.springframework.web.servlet.ModelAndView; + - Import Staticorg.springframework.test.web.servlet.request.mockmvcrequestbuilders.*; the * Import Staticorg.springframework.test.web.servlet.result.mockmvcresultmatchers.*; $ Panax Notoginseng /** - the * Created with IntelliJ idea. + A the * date:14-9-25 + - * Time: PM 6:45 $ $ * To change this template use File | Settings | File Templates. - - */ the -@RunWith (Springjunit4classrunner.class)Wuyi the @WebAppConfiguration - Wu //@ContextConfiguration (classes = {webmvcconfig.class, mockdataconfig.class}) - About@ContextConfiguration (locations={"Classpath:/spring/applicationcontext.xml", "classpath*: Mvc-dispatcher-servlet.xml "}) $ - Public classTESTMOCKMVC { - - @Autowired A + PrivateOrg.springframework.web.context.WebApplicationContext context; the - Mockmvc Mockmvc; $ the @Before the the Public voidbefore () { the - //all controllers can be tested . in theMOCKMVC =Mockmvcbuilders.webappcontextsetup (context). build (); the About //test only for a single controller the the //MOCKMVC = Mockmvcbuilders.standalonesetup (New Meuncontroller ()). build (); the + } - the @TestBayi the Public voidTestgetmenu () { the - Try { - theSystem.out.println ("----------------------------"); the theResultactions actions = the - This. Mockmvc.perform (Get ("/menu/manage.action")); the theSystem.out.println (status ());// the 94 System.out.println (Content (). toString ()); the the actions.andexpect (Status (). IsOk ()); the 98 //Actions.andexpect (Content (). ContentType ("text/html")); About -System.out.println ("----------------------------");101 102}Catch(Exception e) {103 104E.printstacktrace ();//To change body of catch statement use File | Settings | File Templates. the 106 }107 108}
View Code
If you want to use testng, you can refer to http://www.51testing.com/html/26/n-856526.html
If you want to use Spock, you can refer to Http://stackoverflow.com/questions/9811345/how-to-inject-spring-beans-into-spock-test
Go [Java] Summary of unit tests using spring with JUnit