- 1 .首先写测试公共类随意放,别的测试类直接继承它
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* 测试共公类
* @author yjj
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-context.xml")
public class SpringJunitTest {
}
- 2 .引入的classpath:application-context.xml指的是自己配置文件中除了web.xml文件外需要引入的配置文件,本文件在src/test/resources下,在src/test/java下写代码,且包名文件名最好一样eg:类PeopleService–TestPeopleService方法save—saveTest这样能较清晰,其内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<import resource="config/*.xml" />
</beans>
- 3 .在resources下创建config文件夹然后把需要加载的xml文件放到文件目录下即可。
- 4 .示例测试文件
public class FileManagerTest extends SpringJunitTest{
@Autowired
private FileManager fileManager;
@Test
public void getPeople(){
try {
Test1 test = fileManager.getPeople("57b443e68406d0e9bcc1f338");
Map covertValue = JsonUtils.covertValue(test, Map.class);
BeanInfo beanInfo = Introspector.getBeanInfo(Test1.class);
PropertyDescriptor[] propertyDesc = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDesc) {
System.out.println(propertyDescriptor.getName()+"----"+covertValue.get(propertyDescriptor.getName()));
}
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void addPeople(){
Test1 test = new Test1();
test.setAge(23);
test.setBr(true);
test.setDate(new Date());
test.setName("yjj");
test.setPassword("123123");
test.setTime(new Timestamp(new Date().getTime()));
try {
fileManager.addPeople(test);
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
}