Spring consolidates JUnit tests

Source: Internet
Author: User

The content of this section:

    • The meaning of spring's integration with JUnit testing
    • Spring consolidates JUnit tests

The meaning of spring and integrated JUnit testing

Prior to the integration of JUnit, we wrote the test method by manually creating the container in each method, getting the object, such as the following code, the red part is the duplicated code. If you want to test a lot of features, you have to manually create the container each time, very troublesome. If you are testing two functions using one of the same objects, get the code for the object to be written again.

 Public classTest {@Test Public voidtest1 () {//1. Create a Container object (Create a spring factory Class)        ApplicationContext ac = new Classpathxmlapplicationcontext ("Com/wisedu/annotation/applicationcontext.xml");// Classpathxmlapplicationcontext (the application container for loading XML from a classpath) is the implementation class of Org.springframework.context.ApplicationContext//        2. The User user = (user) Ac.getbean ("user") to the container "want" user object (Fetch bean instance through factory parsing XML); //3. Print the User ObjectSystem.out.print (user); } @Test Public voidtest2 () {//1. Create a Container object (Create a spring factory Class)        ApplicationContext ac = new Classpathxmlapplicationcontext ("Com/wisedu/annotation/applicationcontext.xml");// Classpathxmlapplicationcontext (the application container for loading XML from a classpath) is the implementation class of Org.springframework.context.ApplicationContext// 2. To the container "want" user object (Get bean instance through factory parsing xml) User user = (user) Ac.getbean ("User"); User User2= (user) Ac.getbean ("user")); User User3= (user) Ac.getbean ("user")); //3. Print the User ObjectSystem.out.print (user==user2); }}

Spring is more thoughtful, and spring consolidates junit tests to use the container's objects in test code in a more convenient way. This knowledge point is only to be more convenient in testing, not to use it is not related.

II. Spring Integration JUnit test

1. Need to introduce a new jar package

Spring-test-4.2.4.release.jar

2. Using annotations

@RunWith (Springjunit4classrunner.  Class// help us create a container

However, because the spring configuration file location and name are arbitrary, you have to indicate the location and name of the configuration file.

// specify which file to use when creating the container // @ContextConfiguration (locations = "Classpath:com/wisedu/annotation/applicationcontext.xml")

In addition, the object is manually fetched in each test method, and now we can use the value to inject the variable:

@Resource (name = "User")private user u;

To write a test method:

@Test  Public void test3 () {    System.out.print (u);}

There is no need to manually create containers and get objects manually in each test method.

The entire code is as follows:

@RunWith (Springjunit4classrunner.class)//help us create a container@ContextConfiguration ("Classpath:com/wisedu/annotation/applicationcontext.xml")//specify which file to use when creating the container//@ContextConfiguration (locations = "Classpath:com/wisedu/annotation/applicationcontext.xml") Public classTest {//injecting an object named user into the U variable@Resource (name = "User")    PrivateUser u; @Test Public voidtest3 () {System.out.print (U); }}

Spring consolidates JUnit tests

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.