junit4 单元测试

1.所需jar包

下载地址:http://search.maven.org/

搜索junit-4.12-beta-1

  aspectjweaver-1.8.2

  spring-test-4.0.6.RELEASE

2.3中测试形式

(1)最古老的

package sy.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import sy.model.User;
import sy.service.IUserService;

//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"})
public class TestMybatis {
    
    //使用test注解
    @Test
    public void test1(){
        //读取配置文件,如有多个,用逗号隔开
        ApplicationContext ctf = 
                new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
        //获取userService实例
        IUserService userService = (IUserService) ctf.getBean("userService");
        User user = userService.getUserById("0");
        System.out.println(user);
    }
    
}

  (2).如果有多个test,则spring每次都要去加载读取上下文,很浪费资源,因此

package sy.test;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import sy.model.User;
import sy.service.IUserService;

//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"})
public class TestMybatis {
    
    private IUserService userService;
    private ApplicationContext ctf;
    @Before
    public void before(){
        ctf = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
         userService = (IUserService) ctf.getBean("userService");
    }
    
    //使用test注解
    @Test
    public void test1(){
        User user = userService.getUserById("0");
        System.out.println(user);
    }
    
    //使用autowired注解
    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
    
    
}

@Before注解会在所有test测试之前执行

(3)spring与junit整合

package sy.test;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import sy.model.User;
import sy.service.IUserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"})
public class TestMybatis {
    
    private IUserService userService;
    
    //使用test注解
    @Test
    public void test1(){
        User user = userService.getUserById("0");
        System.out.println(user);
    }
    
    //使用autowired注解
    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
    
    
}

 

转载于:https://www.cnblogs.com/ruo-/p/3950176.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值