单元测试
来源:互联网 发布:小米智能家庭软件 编辑:程序博客网 时间:2024/05/22 12:53
读取配置文件进行单元测试
package com.imooc.springData.dao;import java.util.List;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.imooc.springData.entity.Student;public class StudentDaoImplTest {private ApplicationContext ctx = null;private StudentDao studentDao = null;@Beforepublic void setup(){ctx = new ClassPathXmlApplicationContext("beans.xml");studentDao = (StudentDao) ctx.getBean("studentDao");System.out.println("setup");}@Afterpublic void tearDown(){ctx = null;System.out.println("tearDown");}@Testpublic void testQuery(){List<Student> students = studentDao.query();for(Student student : students){System.out.println("id:"+student.getId()+" , name:" +student.getName()+" , age:" + student.getAge());}}@Testpublic void testSave(){Student student = new Student();student.setName("test");student.setAge(30);studentDao.save(student);}}
package com.imooc.springData.util;import javax.sql.DataSource;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.util.Assert;public class DataSourceTest {private ApplicationContext ctx = null;@Beforepublic void setup(){ctx = new ClassPathXmlApplicationContext("beans.xml");System.out.println("setup");}@Afterpublic void tearDown(){ctx = null;System.out.println("tearDown");}@Testpublic void testDataSource(){DataSource dataSource =(DataSource)ctx.getBean("dataSource");Assert.notNull(dataSource);}@Testpublic void testJdbcTemplate(){System.out.println("testJdbcTemplate");JdbcTemplate jdbcTemplate =(JdbcTemplate)ctx.getBean("jdbcTemplate");Assert.notNull(jdbcTemplate);}}
beans.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><!-- Spring 数据库连接 --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="username" value="root" /><property name="password" value="123456" /><property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_data" /></bean><!-- Spring jdbcTemplate配置 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource" /></bean><bean id="studentDao" class="com.imooc.springData.dao.impl.StudentDaoImpl"><property name="jdbcTemplate" ref="jdbcTemplate" /></bean></beans>
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = Application.class) @WebAppConfiguration public class SupervisionHandleServiceTest { @Autowired private Service service; @Test public void testPageList() throws Exception { List<> bjList = service.pageList("1", "10"); Assert.assertNotNull(bjList); } @Test public void testSave() throws Exception { boolean flag = service.save("83b9a7456f8", "cccccccccccccccccccc"); Assert.assertTrue(flag); } @Test public void testGetList() throws Exception { Map<String, Object> Params = new HashMap<String, Object>(); searchParams.put("AITEMNAME", ""); searchParams.put("D", ""); List<> bjList = service.getList(Params, "2017-01-01", "2017-09-05", "1", "10"); Assert.assertNotNull(bjList); }
阅读全文
0 0
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试:
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 移动开发滑动隐藏导航条功能
- Webpack 入门教程
- TensorFlow学习笔记(UTF-8 问题解决 UnicodeDecodeError- 'utf-8' codec can't decode byte 0xff in position 0- in
- Java Web学习总结35:使用JDBC处理Oracle大数据
- 18:鸡尾酒疗法
- 单元测试
- HTML标签总结
- Python之创建单元素tuple
- 分层窗口(Layered windows)翻译
- gulp使用指南
- display:none,overflow:hidden,visibility:hidden之间的区别
- PHP代码安全检测
- C语言字符移位过程
- hibernate Null value was assigned to a property of primitive type setter