"Go" springtest framework junit Unit test Case Get ApplicationContext instance method

Source: Internet
Author: User

Transferred from: Http://www.coderli.com/junit-spring-test-applicationcontext

The spring framework is used in JUnit unit test cases, directly as follows.

@RunWith (Springjunit4classrunner.  Class= {"/spring/applicationcontext.xml" })publicclass Springtest {}

You want to get the ApplicationContext instance. You can add an injection to the ApplicationContext

Runwith (Springjunit4classrunner.  Class= {"/spring/applicationcontext.xml" })publicclass  springtest {@ autowiredprotected applicationcontext ctx;

Spring has already provided a more convenient base class directly: Abstractjunit4springcontexttests

@ContextConfiguration (locations = {"/spring/applicationcontext.xml") }) Public classSpringtestextendsabstractjunit4springcontexttests { Public<T> T Getbean (class<t>type) {returnApplicationcontext.getbean (type);}  PublicObject Getbean (String beanname) {returnApplicationcontext.getbean (beanname);} protectedApplicationContext GetContext () {returnApplicationContext;} }

I add:

If you need a reference in a static class, you can only use the following methods:

 Public classTplatforminfotestextendstestbase{Private Staticmyhttpclient Tplatforminfodispose; @BeforeClass Public Static voidSetupbeforeclass () {System.out.println ("***********here init first***************"); Tplatforminfodispose=NewMyhttpclient ("Platform"); Datasourcecontextholder.setdbtype ("DS2"); ApplicationContext CTX=NewClasspathxmlapplicationcontext ("Classpath:web-inf/rest-*.xml"); PFM= Ctx.getbean (tplatforminfomapper.class); Dim= Ctx.getbean (tdeviceinfomapper.class); }

"Go" springtest framework junit Unit test Case Get ApplicationContext instance method

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.