Original: Spring integrates the JUnit test framework (easy tutorial based on MyEclipse, no hassle in guide package)

Source: Internet
Author: User

I used MyEclipse 10, and I've been trying to use JUnit to test class methods that contain spring annotations or dynamic injections, but because of the relevant jar files found on the web for testing, always reported such errors, today inadvertently found that MyEclipse has brought Spring is based on JUnit's test framework and is very useful.

1. Needless to say, first add the test framework's Class Library: Project-->buildpath-->addlibraries-->myelipse libraries-->spring 2.5 Testing Support libraries (I use 2.5, the specific version is selected by the spring version of your own project)-->finish. At this point, all the relevant jar packages of the test framework are added to the project.

There are two more class libraries in this project

2. Start writing our test cases. On the code:

Package Com.m6699.cyt.dao.jdbc.oracle;import static Org.junit.assert.*;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner, @RunWith (Value=springjunit4classrunner.class) @ Contextconfiguration (locations={"Classpath*:/spring.xml", "Classpath*:/spring-security.xml", "classpath*:/ Spring-quartz.xml "}) public class Jdbcoracleorderdaotest {    @Testpublic void Testchangestate () {System.out.println ("Quasi-United States, can finally junit test");//fail ("not yet implemented");}}

Note: @RunWith
@ContextConfiguration

The equivalent of a spring-loaded configuration file, and a test class.

These two annotations are added with detailed usage, the articles related to the Web are written a lot, no longer to be described.

3. Then we run our test cases and show the Green Pass, and the junit that supports spring injection runs successfully. (You may encounter problems, such as the @postconstruct class found at load time, unable to load the injection, then comment out this is good)

The use of @postconstrutct is quite large, see in detail: http://blog.csdn.net/yaerfeng/article/details/8447530

Original: Spring integrates the JUnit test framework (easy tutorial based on MyEclipse, no hassle in guide package)

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.