2012-08-30 76 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() –