junit 测试 这是什么错误啊

司马他 2011-11-02 10:46:33
机的运行成功过忽然之间就不行了
package junit;

import junit.framework.Assert;
import junit.framework.TestCase;

public class arrayTest extends TestCase{

array array;

public void setUp ()
{
array = new array();
}

public void testGetLargest(){
int[] a={3,4,5,6};
int result = 0;
try {

result = array.getLargest(a);

} catch (Exception e) {

e.printStackTrace();
Assert.fail("test fail");
}
Assert.assertEquals(6, result);
}

public void testgetLargest2(){
Throwable tx = null;
int[] a = {};
try{
array.getLargest(a);
Assert.fail("test");
}
catch(Exception ex){
tx = ex;
}
Assert.assertNotNull(tx);
Assert.assertEquals(Exception.class, tx.getClass());
Assert.assertEquals("array can't be null",tx.getMessage());
}

// public void testgetLargest3(){
// Throwable tx = null;
// int[] a = null;
// try{
// array.getLargest(a);
// Assert.fail();
// }catch(Exception ex){
// tx = ex;
// }
// Assert.assertNotNull(tx);
// Assert.assertEquals(Exception.class, tx.getClass());
// Assert.assertEquals("length is null", tx.getMessage());
// }


}


package junit;

public class array {



public int getLargest(int[] array) throws Exception
{

if (array == null||array.length == 0){
throw new Exception("The array can't be null");
}
else
{
int largest = array[0];
for(int i=0;i<array.length;i++){
if(largest < array[i]){
largest = array[i];
}
}
return largest;
}
}

}



错误是:
arrayTest
junit.arrayTest
testgetLargest2(junit.arrayTest)
junit.framework.ComparisonFailure: null expected:<[]array can't be null> but was:<[The ]array can't be null>

at junit.framework.Assert.assertEquals(Assert.java:81)

at junit.framework.Assert.assertEquals(Assert.java:87)

at junit.arrayTest.testgetLargest2(arrayTest.java:42)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at junit.framework.TestCase.runTest(TestCase.java:168)

at junit.framework.TestCase.runBare(TestCase.java:134)

at junit.framework.TestResult$1.protect(TestResult.java:110)

at junit.framework.TestResult.runProtected(TestResult.java:128)

at junit.framework.TestResult.run(TestResult.java:113)

at junit.framework.TestCase.run(TestCase.java:124)

at junit.framework.TestSuite.runTest(TestSuite.java:243)

at junit.framework.TestSuite.run(TestSuite.java:238)

at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)



...全文
193 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
小笨熊 2011-11-03
  • 打赏
  • 举报
回复
实际值和期望值不一致

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧