0

現在、病院管理システムの JUnit テストを行っています。しかし、insertDoctor メソッドのブール値をチェックしようとすると、JUnit テストで Null Point Exception が発生しました。その点 Statement stmt=con.createStatement. 以下のすべてのコードを添付しました。確認してください。

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));
    }
}

ここに画像の説明を入力


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;                       
    }
}
4

1 に答える 1

1
Statement stmt = con.createStatement();

ここでconはnullです。現在、conの値はコンストラクターで設定されています。それは言う

con = DBConnector.getConnection();

したがって、間違いなく、DBConnector.getConnection()はnullを返します。JUnitでDBConnector()をインスタンス化する必要があります。DBConnectorに関する詳細情報を提供する

于 2012-08-30T18:36:36.123 に答える