2012-08-30 75 views
0

我目前正在为我的医院管理系统进行JUnit测试。但是当试图检查插入的DoDo方法空值点异常的布尔值来在JUnit测试中。那点声明stmt = con.createStatement。我附上了所有的代码。检查它。Junit测试零点排除

public class JUnitTest extends TestCase { 
    private Connection con; 
    public JUnitTest(String testName) { 
     super(testName); 
     con=DBConnector.getConnection(); 
    } 

    @Override 
    protected void setUp() throws Exception { 
     super.setUp(); 
    } 

    @Override 
    protected void tearDown() throws Exception { 
     super.tearDown(); 
    } 
    // TODO add test methods here. The name must begin with 'test'. For example: 
    // public void testHello() {} 
    public void testdeltePatient() throws SQLException{ 

     Patients p=new Patients(); 
     assertEquals(true, p.removePatient(333)); 

    } 
    public void testdeleteDoctor() throws SQLException{ 

     Doctors d=new Doctors(); 
     d.setSpeciality("General"); 
     d.setSumOfQn("MBBS"); 
     d.setExp("3 Years"); 


     d.setDocId(678); 
     d.setEmpId(4344); 

     assertEquals(true, d.insertDoctor(d)); 
    } 
} 

enter image description here


public class Doctors extends Employee { 

    private int docId; 
    private String exp; 
    private String sumOfQn; 
    private int empId; 
    private String speciality; 
    private Connection con; 

    public String getSpeciality() { 
     return speciality; 
    } 

    public void setSpeciality(String speciality) { 
     this.speciality = speciality; 
    } 

    public Doctors() { 

     con = DBConnector.getConnection(); 
    } 

    public int getDocId() { 
     return docId; 
    } 

    public void setDocId(int docId) { 
     this.docId = docId; 
    } 

    public String getExp() { 
     return exp; 
    } 

    public void setExp(String exp) { 
     this.exp = exp; 
    } 

    public String getSumOfQn() { 
     return sumOfQn; 
    } 

    public void setSumOfQn(String sumOfQn) { 
     this.sumOfQn = sumOfQn; 
    } 

    @Override 
    public int getEmpId() { 
     return empId; 
    } 

    @Override 
    public void setEmpId(int empId) { 
     this.empId = empId; 
    } 

    public Connection getCon() { 
     return con; 
    } 

    public void setCon(Connection con) { 
     this.con = con; 
    } 

    public ResultSet getEmployeeId() throws SQLException { 

     ResultSet rs; 
     String query = "select * from employee"; 
     Statement stmt = con.createStatement(); 
     rs = stmt.executeQuery(query); 

     return rs; 

    } 

    public boolean insertDoctor(Doctors d) throws SQLException { 


     PreparedStatement ps; 
     boolean result = false; 
     **Statement stmt = con.createStatement();** 

     String query = "INSERT INTO doctors VALUES ('" + d.getDocId() + "','" + d.getSpeciality() + "','" + d.getExp() + "','" + d.getEmpId() + "',' " + d.getSumOfQn() + "')"; 
     ps = con.prepareStatement(query); 
     int res = ps.executeUpdate(); 
     if (res > 0) { 
      result = true; 
     } 

     return result;      
    } 
} 
+0

看起来'DBConnector.getConnection()'必须在'Doctors' ctor中返回'null'。 –

+0

数据库连接正常,但junit只能工作。 –

+0

尝试在行引发空指针异常之前打印出'con'的值。更可能它是空的,这就是为什么当试图从它创建一个语句时抛出异常。 – Sticks

回答

1
Statement stmt = con.createStatement(); 

这里CON为空。 现在con的值正在构造函数中设置。其中说

con = DBConnector.getConnection(); 

因此,绝对,DBConnector.getConnection()返回null。你必须在Junit中实例化DBConnector()。提供有关DBConnector的更多信息

+0

你能否请说明如何在Junit中声明con变量。 –

+0

使每个功能都足够自我。所以在insertxxxxx函数中调用con = DBConnector.getConnection() –