62,614
社区成员
发帖
与我相关
我的任务
分享
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)