public class CourseSessionTest extends junit.framework.TestCase
{
public void testCreate(){
CourseSession session = new CourseSession("ENGL","101");
Student student1 = new Student("mixian");
Student student2 = new Student("longquan");
session.enroll(student1);
session.enroll(student2);
java.util.ArrayList<Student> studentList =session.getAllStudentList();
assertEquals(student1,studentList.get(0));
assertEquals(student2,studentList.get(1));
assertEquals(2,studentList.size());
assertEquals("ENGL",session.getName());
assertEquals("101",session.getNumber());
assertEquals(2,session.getNumberofstudent());
CourseSession session2 = new CourseSession("CHIN","102");
assertEquals("CHIN",session2.getName());
assertEquals("102",session2.getNumber());
//assertEquals(2,session2.getNumberofstudent());
}
}
前面已经介绍了基本的测试方法,继续java基础的介绍
有学生类以后还要有课程类,用来记录课程名称、编号、学生人数、学分等
public class CourseSessionTest extends junit.framework.TestCase
{
public void testCreate(){
CourseSession session = new CourseSession("ENGL","101");
assertEquals("ENGL",session.getName());
assertEquals("101",session.getNumber());
assertEquals(0,session.getNumberofstudent());
CourseSession session2 = new CourseSession("CHIN","102");
assertEquals("CHIN",session2.getName());
assertEquals("102",session2.getNumber());
assertEquals(1,session2.getNumberofstudent());
}
}
class CourseSession{
private String name;
private String number;
private int studentnumber=0;
CourseSession(String name,String number){
this.name=name;
this.number = number;
}
public void enroll(Student student){
studentnumber++;
}
public void setName(String name){
this.name = name;
}
public void setNumber(String number){
this.number = number;
}
public String getName(){
return this.name;
}
public String getNumber(){
return this.number;
}
public int getNumberofstudent(){
return studentnumber;
}
}
这样需要引入Student这个类
然后更改测试CourseSessionTest类
public class CourseSessionTest extends junit.framework.TestCase
{
public void testCreate(){
CourseSession session = new CourseSession("ENGL","101");
Student student1 = new Student("mixian");
Student student2 = new Student("longquan");
session.enroll(student1);
session.enroll(student2);
assertEquals("ENGL",session.getName());
assertEquals("101",session.getNumber());
assertEquals(2,session.getNumberofstudent());
CourseSession session2 = new CourseSession("CHIN","102");
assertEquals("CHIN",session2.getName());
assertEquals("102",session2.getNumber());
// assertEquals(1,session2.getNumberofstudent());
}
}
class CourseSession{
private String name;
private String number;
private int studentnumber=0;
CourseSession(String name,String number){
this.name=name;
this.number = number;
}
public void enroll(Student student){
studentnumber++;
}
public void setName(String name){
this.name = name;
}
public void setNumber(String number){
this.number = number;
}
public String getName(){
return this.name;
}
public String getNumber(){
return this.number;
}
public int getNumberofstudent(){
return studentnumber;
}
}
明显是由于在类中没有引入Student类。修改一下就好了
现在系统中有2个测试用例 一个是StudentTest.java 和CourseSessionTest.java
为了确保测试都可以同时运行成功,所以要用到测试组件,将2个TEST全部加入到测试组件中一起运行。
public class AllTest{
public static junit.framework.TestSuite suite = new
junit.framework.TestSuite();
suite.add(StudentTest.class);
suite.add(CourseSessionTest.class);
reaturn suite;
}
创建套件并返回,有junit.frameword.TestSuite来管理套件
suite必须是static静态的
稳定版本:
class CourseSession{
private String name;
private String number;
private int studentnumber=0;
private java.util.ArrayList<Student> studentList = new
java.util.ArrayList<Student>();
CourseSession(String name,String number){
this.name=name;
this.number = number;
}
public java.util.ArrayList<Student> getAllStudentList(){
return studentList;
}
public void enroll(Student student){
studentnumber++;
studentList.add(student);
}
public void setName(String name){
this.name = name;
}
public void setNumber(String number){
this.number = number;
}
public String getName(){
return this.name;
}
public String getNumber(){
return this.number;
}
public int getNumberofstudent(){
return studentnumber;
}
}
发现很多地方可以重构:
1:学生数量 不需要单独定义变量只需要过去List的长度
如果在两个或2个以上的方法中需要创建一个类的时候,可以定义成员变量,然后用setup()方法提前初始化,这样的好处就是只需要初始化一次。如代码所示:
public class CourseSessionTest extends junit.framework.TestCase
{
private CourseSession session;
public void setUp(){
session = new CourseSession("ENGL","101");
}
public void testCreate(){
//CourseSession session = new CourseSession("ENGL","101");
Student student1 = new Student("mixian");
Student student2 = new Student("longquan");
session.enroll(student1);
session.enroll(student2);
java.util.ArrayList<Student> studentList =session.getAllStudentList();
assertEquals(student1,studentList.get(0));
assertEquals(student2,studentList.get(1));
assertEquals(2,studentList.size());
assertEquals("ENGL",session.getName());
assertEquals("101",session.getNumber());
//assertEquals(2,session.getNumberofstudent());
/*
CourseSession session2 = new CourseSession("CHIN","102");
assertEquals("CHIN",session2.getName());
assertEquals("102",session2.getNumber());
//assertEquals(2,session2.getNumberofstudent());
*/
}
}
在这里将测试招收学生的方法分离出来
public class CourseSessionTest extends junit.framework.TestCase
{
private CourseSession session;
public void setUp(){
session = new CourseSession("ENGL","101");
}
public void testEnroll(){
Student student1 = new Student("mixian");
Student student2 = new Student("longquan");
session.enroll(student1);
session.enroll(student2);
java.util.ArrayList<Student> studentList =session.getAllStudentList();
assertEquals(student1,studentList.get(0));
assertEquals(student2,studentList.get(1));
assertEquals(2,studentList.size());
}
public void testCreate(){
//CourseSession session = new CourseSession("ENGL","101");
assertEquals("ENGL",session.getName());
assertEquals("101",session.getNumber());
//assertEquals(2,session.getNumberofstudent());
/*
CourseSession session2 = new CourseSession("CHIN","102");
assertEquals("CHIN",session2.getName());
assertEquals("102",session2.getNumber());
//assertEquals(2,session2.getNumberofstudent());
*/
}
}
然后你发现测试招收学生的方法过于繁琐,所以重构后的方式是删除session.getStudentList();方法在Student中增加一个GET方法,避免student列表整个暴露出来
重构后的代码如下:
public class CourseSessionTest extends junit.framework.TestCase
{
private CourseSession session;
public void setUp(){
session = new CourseSession("ENGL","101");
}
public void testEnroll(){
Student student1 = new Student("mixian");
Student student2 = new Student("longquan");
session.enroll(student1);
session.enroll(student2);
//重构删除 避免整个list暴露出来
//java.util.ArrayList<Student> studentList =session.getAllStudentList();
//assertEquals(student1,studentList.get(0));
//assertEquals(student2,studentList.get(1));
//assertEquals(2,studentList.size());
assertEquals(student1,session.get(0));
assertEquals(student2,session.get(1));
assertEquals(2,session.getNumberofstudent());
}
public void testCreate(){
//CourseSession session = new CourseSession("ENGL","101");
assertEquals("ENGL",session.getName());
assertEquals("101",session.getNumber());
//assertEquals(2,session.getNumberofstudent());
/*
CourseSession session2 = new CourseSession("CHIN","102");
assertEquals("CHIN",session2.getName());
assertEquals("102",session2.getNumber());
//assertEquals(2,session2.getNumberofstudent());
*/
}
}