美文网首页游戏开发
Junit单元测试--springTest和原生

Junit单元测试--springTest和原生

作者: higher2017 | 来源:发表于2016-12-11 15:34 被阅读28次
    使用spring-test与junit整合进行单元测试:
    import java.util.List;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.jm.dao.TestViewMapper;
    import com.jm.model.Bug;
    import com.jm.model.TestView;
    import com.jm.service.UserServiceI;
    
    //让测试运行于[spring](http://lib.csdn.net/base/javaee)测试环境
    @RunWith(SpringJUnit4ClassRunner.class)
    //载入spring的xml配置文件
    @ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring-mybatis.xml" })
    @Transactional  
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
    public class TestUserService {
        @Autowired
        private UserServiceI userService;
        @Autowired
        private TestViewMapper testViewMapper;
    
        @Test
        public void testTbug() {
             Bug tbug = userService.getBugById(1L);
             System.out.println(tbug.gettBugId());
            List<TestView> tvServiceList = userService.getTestView();
            
            List<TestView> tvDaoList = testViewMapper.loadTestView();
            for (TestView tv : tvServiceList) {
                System.out.println(tv);
            }
            for (TestView tv : tvDaoList) {
                System.out.println(tv);
            }
        }
    }
    
    以上需要的主要jar包:
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>3.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11-beta-1</version>
                <scope>test</scope>
            </dependency>
    
    使用junit原生进行单元测试:
    import java.util.List;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.jm.dao.TestViewMapper;
    import com.jm.model.Bug;
    import com.jm.model.TestView;
    import com.jm.service.UserServiceI;
    
    @Test
    public class TestUserService {
        @Autowired
        private UserServiceI userService;
        @Autowired
        private TestViewMapper testViewMapper;
        //表示spring上下文
        private ApplicationContext ac;
        
        /**
         * before:执行任意@Test单元方法之前所要执行的方法
         * 
         * @author JM 2016-12-1 下午10:05:02
         */
        @Before
        public void before() {
            ac = new ClassPathXmlApplicationContext(new String[] { "spring.xml", "spring-mybatis.xml" });
            userService = (UserServiceI) ac.getBean("userService");
            testViewMapper = (TestViewMapper) ac.getBean("userService");
        }
    
        @Test
        public void testTbug() {
            Bug tbug = userService.getBugById(1L);
            System.out.println(tbug.gettBugId());
            List<TestView> tvServiceList = userService.getTestView();
    
            List<TestView> tvDaoList = testViewMapper.loadTestView();
            for (TestView tv : tvServiceList) {
                System.out.println(tv);
            }
            for (TestView tv : tvDaoList) {
                System.out.println(tv);
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:Junit单元测试--springTest和原生

        本文链接:https://www.haomeiwen.com/subject/ivjjmttx.html