java - Junit 测试空指针异常

标签 java unit-testing junit

我目前正在为我的医院管理系统进行 JUnit 测试。但是当尝试检查 insertDoctor 方法的 boolean 值时,JUnit 测试中出现空点异常。那点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));
    }
}
<小时/>

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

最佳答案

Statement stmt = con.createStatement();

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

con = DBConnector.getConnection();

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

关于java - Junit 测试空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12202771/

相关文章:

typescript - Jest 模拟节点模块不适用于 typescript

spring - 在 Spring Boot 应用程序中运行 JUnit 单元测试而不提供数据源

java - 单元测试 Google App Engine 1.6.4 中出现非法 blobKey 错误

java - 如何检查 ConcurrentLinkedQueue 的 size() 或 isEmpty()

python - Pycharm 在使用 Python 的 pytest 框架进行测试时为测试生成奇怪的代码模板

java - 从字符串中删除设置的子字符串

java - 如何验证方法内部正在启动的方法?

java - 为什么 BeforeTransaction 和 AfterTransaction 方法没有触发?

java - Android Studio 1.0.1 重复文件复制到 APK META-INF/DEPENDENCIES

java - 从文件流导入 H2 CSV