File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Junit testing for overflow, how to prevent Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Junit testing for overflow, how to prevent" Watch "Junit testing for overflow, how to prevent" New topic
Author

Junit testing for overflow, how to prevent

Scott Pike
Greenhorn

Joined: Jan 17, 2007
Posts: 12
I don't know if this is the correct forum, but here is my problem. I'm learning and I'm trying to use a test Junit with my method that takes numbers from a collection and gives me an average. I got it to the point where the numbers are taken from the element of the collection and placed in a double variable and added, but one of the Junit test test to see what will happen with an null value in the element.. I tried doing an if(null == smoething ) test but I can seem to figure out what will work. what does the value have to be in for me to compare it and see if it is a null?


Junit

My Program


[ September 20, 2007: Message edited by: Jim Yingst ]
mohsen fakhari
Greenhorn

Joined: May 04, 2003
Posts: 25
Hi scott
I read your question but I'm not sure about your poblem.
At first I should say it's hard to understand your code without indentation.
Your code will cause NullPointerException because you should put
sum = sum +aNumber.doubleValue();
in an 'else' statement.
and your 'size' variable shows a wrong value because it consists 'null' values too.
Scott Pike
Greenhorn

Joined: Jan 17, 2007
Posts: 12
thanks for the thing about my sum addition, but my thing is supposed to contain a null value. This is a testing phase and I am trying to find a way for the program to test to see if an elemen has a null value and then display a message that there is a null value and stopping th program.

in other words, after it gets a null, I want it to test the variable for a null and then give out an error message and stop the program. but so far I havent figured out how to test it for a null.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
The way you are testing for null is correct. The problem is that you get a NullPointerException before you reach the test.


Joanne
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I added code tags to your code to make it more readable; please use these in the future.

I assume you tried running the code above and got a NullPointerException. The stack trace in this exception should tell you which line is causing the problem. As Joanne points out, you need to test for null before the line that's throwing the NullPointerException.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Junit testing for overflow, how to prevent
 
Similar Threads
Output for Arrays.binarySearch()
Counting in Array List/ Standard deviation
Generics
ways to deal with null poingt exceptions
basic collection question