I am trying to make a JUnit test. But I can't make my exceptions work. Both
the throw and the try statements don't work, does someone have some ideas?
JS
import junit.framework.*;
class Average
{
public static float average(float[] nums)
{
float b = 0;
if(nums.length == 0)
throw new Exception("Need to be a list with elements");
else
{for (int i = 0; i < nums.length; i++)
{ b += nums[i];}
return b/nums.length;}
}
}
public class AverageTest extends TestCase
{
public void test1()
{
float[] f = new float[0];
try{Average.average(f, fail("dfgdg"));}
catch(Exception e){}
}
public void test2()
{
float[] p = {1,2,3,6};
assertTrue("gggg", Average.average(p) == 3);
}
}
Andrew McDonagh - 03 Jan 2005 19:39 GMT
> I am trying to make a JUnit test. But I can't make my exceptions work. Both
> the throw and the try statements don't work, does someone have some ideas?
[quoted text clipped - 16 lines]
> }
> }
You are trying to throw a Checked exception, yet the method signature
does not specify that a checked exception could be thrown.
So either add the 'throws Exception' clause to the end of the method
signature or a preferred way is to throw an Unchecked exception - e.g.
IndexOutOfBoundsException("...");
Also, because you are throwing a specific exception your tests can
specifically catch only that exception - so any other exceptions are
confused with what the test is hoping to see.
> public class AverageTest extends TestCase
> {
[quoted text clipped - 5 lines]
> catch(Exception e){}
> }
Because of poor formatting, you have missed the fact that the
fail("dfgdg");
is inside the parameter list of the average() method. As the average()
method only accepts a float[] array, this is the compile error.
What development environment are you using? As both of these are easy
to spot and therefore fix, if you used something like Eclipse or Idea.
Here's your code properly formatted and corrected and the test methods
renamed to be a bit more descriptive.
public class Average {
public static float average(float[] nums) {
float b = 0;
if (nums.length == 0)
throw new IndexOutOfBoundsException("Empty list found");
else {
for (int i = 0; i < nums.length; i++) {
b += nums[i];
}
return b / nums.length;
}
}
}
public class AverageTest extends TestCase {
public void testEmptyArrayThrowsException() {
float[] f = new float[0];
try {
Average.average(f);
fail("dfgdg");
} catch (IndexOutOfBoundsException e) {
// success
}
}
public void testAverage() {
float[] p = { 1, 2, 3, 6 };
assertTrue("gggg", Average.average(p) == 3);
}
}
Andrew McDonagh - 03 Jan 2005 19:42 GMT
> I am trying to make a JUnit test. But I can't make my exceptions work. Both
> the throw and the try statements don't work, does someone have some ideas?
[quoted text clipped - 16 lines]
> }
> }
You are trying to throw a Checked exception, yet the method signature
does not specify that a checked exception could be thrown.
So either add the 'throws Exception' clause to the end of the method
signature or throw an Unchecked exception, preferably the
IndexOutOfBoundsException("...");
Also, because you are throwing a specific exception your tests can
specifically catch only that exception - so any other exceptions are NOT
confused with what the test is hoping to see.
> public class AverageTest extends TestCase
> {
[quoted text clipped - 5 lines]
> catch(Exception e){}
> }
Because of poor formatting, you have missed the fact that the
fail("dfgdg");
is inside the parameter list of the average() method. As the average()
method only accepts a float[] array, this is the compile error.
What development environment are you using? As both of these are easy
to spot and therefore fix, if you used something like Eclipse or Idea.
Here's your code properly formatted and corrected and the test methods
renamed to be a bit more descriptive.
public class Average {
public static float average(float[] nums) {
float b = 0;
if (nums.length == 0)
throw new IndexOutOfBoundsException("Empty list found");
else {
for (int i = 0; i < nums.length; i++) {
b += nums[i];
}
return b / nums.length;
}
}
}
public class AverageTest extends TestCase {
public void testEmptyArrayThrowsException() {
float[] f = new float[0];
try {
Average.average(f);
fail("dfgdg");
} catch (IndexOutOfBoundsException e) {
// success
}
}
public void testAverage() {
float[] p = { 1, 2, 3, 6 };
assertTrue("gggg", Average.average(p) == 3);
}
}