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 ...