java使用JUnit出現java.lang.NullPointerException

冰糖雪梨燉排骨發表於2018-04-09
   今天按照視訊,通過zookeeper來實現伺服器的連線與上線,然後因為沒有定義main方法,所以想通過JUnit來測試一些連線zookeeper伺服器的方法,然後就一直出現返回空指標的錯誤。
程式碼:
package cn.itcast.bigdata.zk;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.xml.internal.security.Init;

import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.client.ZooKeeperSaslClient;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import javafx.scene.chart.PieChart.Data;

public class SimpleClientZk {

private static final String connectString = "shizhan01:2181,shizhan02:2181,shizhan03:2181";
private static final int sessionTimeout = 2000;
ZooKeeper ZkClient = null;

public  void init() throws Exception {
          ZkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
        //收到事件通知後的回撥函式(我們自己的事件處理邏輯) getType():事件型別
System.out.println(event.getType() + "------------" + event.getPath());
try {
ZkClient.getChildren("/", true);
} catch (Exception e) {
}
}
});
}

//建立資料節點到zk中
@Test
public void nodeCreate() throws Exception, InterruptedException {
String nodeCreate = ZkClient.create("/eclips", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
//獲取子節點
@Test
public void getChildren() throws Exception {
List childrennode = ZkClient.getChildren("/", true);
for(String child : childrennode ) {
System.out.println(child);
}
Thread.sleep(Long.MAX_VALUE);
}
//判斷節點是否存在
@Test
public void testExist() throws  Exception {
Stat stat = ZkClient.exists("/eclips", false);
System.out.println(stat == null ? "not exists":"znode exists");
}
//獲取節點資料
@Test
public void getData() throws Exception, InterruptedException {
byte[] data = ZkClient.getData("/eclips", false, null);
System.out.println(new String(data));
}
//刪除節點
@Test
public void deleteNode() throws Exception, KeeperException {
ZkClient.delete("/eclips", -1);
}
//修改節點
@Test
public void setNodeData() throws Exception, InterruptedException {
ZkClient.setData("/app1", "setdata".getBytes(), -1);
byte[] data = ZkClient.getData("/app1", false, null);
System.out.println(new String(data));
}
}
     通過百度查閱一些資料後,終於發現問題所在,因為我的程式碼中所使用的物件是在我自己寫的一個初始化方法init()中對其進行初始化的,而用JUnit測試方法時,方法中的物件都是獨立存在的,所以會一直出現返回空指標的錯誤。
解決方法:在你的初始化方法init()的開頭加一個@Before,用來初始化測試單元中的外部需求,完美解決。
       @Before
public  void init() throws Exception {
          ZkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
        //收到事件通知後的回撥函式(我們自己的事件處理邏輯) getType():事件型別
System.out.println(event.getType() + "------------" + event.getPath());
try {
ZkClient.getChildren("/", true);
} catch (Exception e) {
}
}
});
}

相關文章