@RunWith is used with springjunit4classrunner---->JUNIT4 and Spring

Source: Internet
Author: User

Today, while watching Spring's demo, I saw how the unit test was written

As follows:


@RunWIth (Springjunit4classrunner.class)

@ContextConfiguration (locations = {"Classpath:applicationContext.xml"}

public class MyTest

{

@Test

public void hehe ()

{

//.......

}

}


This is written to allow the test to execute in the spring container environment.

What is Spring's container environment?

Like the common Service Dao Action, which is in the spring container, JUnit needs to get them and use them to test.


Good, I write a very simple demo let everyone have a experience.


Show the project structure of the demo

JUNIT4 and Spring Use ">

What to write is two applicationcontext.xml and Mytest.java.

Only one Date object is defined in the Applicationcontext.xml.


<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=
"Http://www.springframework.org/schema/beans"
       xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemalocation= "http:// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">


    < Bean name= "Date" class= "Java.util.Date"/>


</beans>

JUNIT4 and Spring Use ">

And then there's the Mytest.java content.


Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Import Javax.annotation.Resource;
Import java.util.Date;

/**
 * Created by Huluo on 2016/8/19.
 * *
@RunWith (springjunit4classrunner.class)
@ContextConfiguration (locations = {"Classpath: Applicationcontext.xml "}) public
class MyTest
{
    @Resource
    date date;

    @Test public
    void hehe ()
    {
        System.out.println (date.tolocalestring ());
    }
}

JUNIT4 and Spring Use ">


Finally only need to run on it.

JUNIT4 and Spring Use ">

Finally, as shown in the picture, the Date object in the spring container was successfully obtained.

such as which Action Service Dao Serviceimpl Daoimpl is a truth that can be obtained in this way and then unit test ...

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.