java使用JUnit出现java.lang.NullPointerException

   今天按照视频,通过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) {
}
}
});
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.NullPointerException at com.example.machinepotest.mappertest.tes(mappertest.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725) at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
05-16

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值