Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2005

Tip: Looking for answers? Try searching our database.

Exceptions i JUnit Test

Thread view: 
JS - 03 Jan 2005 16:14 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?

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);
   }

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.