我正在实施一个基于网络服务的大学管理系统。该系统将某些课程添加到数据库中。下面是我正在使用的代码。
课程.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 服务文件,如下所示,它与前两个交互并将数据添加到数据库。
课程服务.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 测试,比如上面的添加课程。
请提出一些我可以学习、阅读和使用的东西来为我的数据库方法实现 Junit 测试。