測試junit類的通用寫法

  • 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) {
            // TODO Auto-generated catch block
            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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章