Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Junit测试空点异常_Java_Unit Testing_Junit - Fatal编程技术网

Java Junit测试空点异常

Java Junit测试空点异常,java,unit-testing,junit,Java,Unit Testing,Junit,我目前正在为我的医院管理系统进行JUnit测试。但当尝试检查insertDoctor方法的布尔值时,JUnit测试中出现了Null点异常。该点语句stmt=con.createStatement。我已附上以下所有代码。请检查一下 public class JUnitTest extends TestCase { private Connection con; public JUnitTest(String testName) { super(testName);

我目前正在为我的医院管理系统进行JUnit测试。但当尝试检查insertDoctor方法的布尔值时,JUnit测试中出现了Null点异常。该点语句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));
    }
}


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

con = DBConnector.getConnection();

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

它看起来像是
DBConnector.getConnection()
必须在
Doctors
ctor.Database Connection中返回
null
,但junit仅不起作用……尝试在抛出null指针异常的行之前打印“con”的值。它很可能是空的,这就是为什么在尝试从它创建语句时抛出异常。您似乎也没有使用您在抛出异常的行中创建的语句(除非该代码是为了提问而被删除的?).你能解释一下我如何在Junit中声明con变量吗?让每个函数都自给自足。所以在insertxxxxx函数中调用con=DBConnector.getConnection()
Statement stmt = con.createStatement();
con = DBConnector.getConnection();