测试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();
        }
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值