我正在实施一个基于Web服务的大学管理系统.该系统将某些课程添加到数据库中.下面是我正在使用的代码.
Course.java
public class Course { private String courseName; private String location; private String courseId; public String getCourseId() { return courseId; } public void setCourseId(String courseId) { this.courseId = courseId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } }
然后另一个文件如下
CourseDaoImpl.java
public class CourseDaoImpl implements IDao { Connection conn = null; Statement stmt = null; public CourseDaoImpl(){ try { Class.forName("com.MysqL.jdbc.Driver").newInstance(); conn = DriverManager.getConnection( "jdbc:MysqL://localhost:3306/univesitydb","root","root"); stmt = conn.createStatement(); if (!conn.isClosed()) System.out.println("Successfully connectiod"); } catch (sqlException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } @Override public String add(Object object) { Course c = (Course) object ; String courseId = c.getCourseId(); String courseName = c.getCourseName(); String location = c.getLocation(); String result = ""; int rowcount; try { String query = "Insert into course (courseId,courseName,location) values" + " ('" + courseId + "','" + courseName + "','" + location + "')"; rowcount = stmt.executeUpdate(query); if (rowcount > 0) { result = "true"; System.out.println("Course inserted successful"); } else { result = "false:The data could not be inserted in the databse"; } } catch (sqlException e) { e.printStackTrace(); } return result; }
第三个是如下的Web服务文件,它与前两个交互并将数据添加到数据库.
CourseService.java
package edu.service; import edu.dao.IDao; import edu.dao.impl.CourseDaoImpl; import edu.db.entity.Course; public class CourseService { public String addCourse(String courseId,String courseName,String location) { Course c = new Course(); c.setCourseId(courseId); c.setCourseName(courseName); c.setLocation(location); IDao dao = new CourseDaoImpl(); return dao.add(c); }
查看我的代码清单,任何机构都可以建议我如何为我的add方法编写测试用例.我是JAVA的初学者,我从朋友那里学习了这个java部分,现在需要为我的数据库方法实现Junit测试,比如上面的添加课程.
解决方法
这是在spring项目中使用junit的一个示例dao测试.
import java.util.List; import junit.framework.Assert; import org.jboss.tools.example.springmvc.domain.Member; import org.jboss.tools.example.springmvc.repo.MemberDao; 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 org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:test-context.xml","classpath:/Meta-INF/spring/applicationContext.xml"}) @Transactional @TransactionConfiguration(defaultRollback=true) public class MemberDaoTest { @Autowired private MemberDao memberDao; @Test public void testFindById() { Member member = memberDao.findById(0l); Assert.assertEquals("John Smith",member.getName()); Assert.assertEquals("john.smith@mailinator.com",member.getEmail()); Assert.assertEquals("2125551212",member.getPhoneNumber()); return; } @Test public void testFindByEmail() { Member member = memberDao.findByEmail("john.smith@mailinator.com"); Assert.assertEquals("John Smith",member.getPhoneNumber()); return; } @Test public void testRegister() { Member member = new Member(); member.setEmail("jane.doe@mailinator.com"); member.setName("Jane Doe"); member.setPhoneNumber("2125552121"); memberDao.register(member); Long id = member.getId(); Assert.assertNotNull(id); Assert.assertEquals(2,memberDao.findAllOrderedByName().size()); Member newMember = memberDao.findById(id); Assert.assertEquals("Jane Doe",newMember.getName()); Assert.assertEquals("jane.doe@mailinator.com",newMember.getEmail()); Assert.assertEquals("2125552121",newMember.getPhoneNumber()); return; } @Test public void testFindAllOrderedByName() { Member member = new Member(); member.setEmail("jane.doe@mailinator.com"); member.setName("Jane Doe"); member.setPhoneNumber("2125552121"); memberDao.register(member); List<Member> members = memberDao.findAllOrderedByName(); Assert.assertEquals(2,members.size()); Member newMember = members.get(0); Assert.assertEquals("Jane Doe",newMember.getPhoneNumber()); return; } }