Springtest Framework junit Unit test Case Get ApplicationContext instance method

Source: Internet
Author: User

The spring framework is used in JUnit unit test cases, which I used very directly.

/** * For test case base classes that need to be used with  spring * @author  http://www.coderli.com*/@RunWith (Springjunit4classrunner. class  = {"/spring/applicationcontext.xml" })publicclass Springtest {}

In the course of the test, it was mentioned that you want to get the ApplicationContext instance. As a result, an injection of ApplicationContext is added.

* ** * * * *  @blog http://www.coderli.com */@RunWith ( Springjunit4classrunner. class  = {"/spring/applicationcontext.xml" })publicclass  springtest {@ autowiredprotected applicationcontext ctx;

In fact, Spring has already directly provided a more convenient base class: Abstractjunit4springcontexttests. Modify the code as follows:

/*** For the base class of test cases where spring is required * *@authorLihzh * @alia onecoder * @bloghttp://www.coderli.com */@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;}}

The code is much more concise.

Now think about the common functions you want, which can be thought of by the general family. Before you do this, you may want to check if there are any tools available:)

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

Springtest Framework junit Unit test Case Get ApplicationContext instance method

Related Article

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.