站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题求助,按照网上介绍的最简单的求绝对值得junit测试出错,使用Eclipse,有经验的过来看看

求助,按照网上介绍的最简单的求绝对值得junit测试出错,使用Eclipse,有经验的过来看看

摘要: 求助,按照网上介绍的最简单的求绝对值得junit测试出错,使用Eclipse,有经验的过来看看


package example;

public class Hello {

/**
* @param args
*/
public int abs (int n){
//return n+1;
return n >=0 ? n:-n;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}



public class HelloTest extends TestCase {
public Hello hello;
protected void setUp() throws Exception {
super.setUp();
}

protected void tearDown() throws Exception {
super.tearDown();
}

/*
* Test method for 'example.Hello.abs(int)'
*/
public void testAbs() {
try{
assertEquals(hello.abs(10), 10);
assertEquals(hello.abs(-14), 14);
assertEquals(hello.abs(0), 0);
}catch(Exception e){
}
}
}

我发现我把testAbs用try{}catch(){}括起来,怎么修改assertEquals(hello.abs(10), 10);的值都显示绿色,反之把try{}catch(){}去掉,不管assertEquals(hello.abs(10), 10);的值怎么正确,都显示红色
报错信息为:
java.lang.NullPointerException
at example.HelloTest.testAbs(HelloTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

怎么回事呀,是我那里配置错误了码?希望有经验的过来看看,谢谢拉!






public Hello hello;

这里错了,要么改
public int abs (int n){
//return n+1;
return n >=0 ? n:-n;
}

public static int abs (int n){
//return n+1;
return n >=0 ? n:-n;
}

要么new 一下hello ,否则肯定会提示NullPointerException


在setup()中添加:
hello=new Hello();//原因是hello默认是null,自然会引发NullPointerException了


↑返回目录
前一篇: 请问Eclipse中将项目打包
后一篇: 请问 为什么Eclipse里面找不到 Run on Sever 的选项