单元测试

来源:互联网 发布:小米智能家庭软件 编辑:程序博客网 时间: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>


springBoot单元测试

@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); }