Results 1 to 18 of 18
  1. #1
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default JUnit test and ArrayIndexOutOfBoundsException

    I didn't know I could do that!

    This is the method that is causing all the problems:

    Java Code:
    public static int numberOfDaysInMonth(int month, int year) {
    		int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    		// if the month is February, and it is a leap year, return 29 instead of 28 
    	    if (month == 2 && isLeapYear(year)) {
    	        return 29;
    	    } else {
    	    	// return the number of days for the month
    	        return daysInMonth[month-1];
    	    }
    	}
    For some reason when I run any JUnit test it always says "array index out of bounds exception: -1". Even when I put in the month as 02 and year as 2012 it does this, even though it shouldn't even be looking for an array index with this particular date. If I change the last part to [month] or [month+1] etc. the 02/2012 test case works! I just cannot understand why it is doing this for [month-1]?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: PM's

    Can you post the exact call to the function that exhibits the error?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: PM's

    This is the February leap year test case:

    Java Code:
    @Test
    	public void testNumberOfDaysInLeapYearFebruary() {
    		int month = 02;
    		int year = 2012;
    		int result = leapYearFebruary.numberOfDaysInMonth(month, year);
    		assertEquals(29, result);
    	}

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    Split from PM's

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    Darryl can you shed any light on what is going wrong?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    Running your code, changing the function call to work from main, and taking out the call to isLeapYear() does not generate the same behavior you're describing. You should post an SSCCE that demonstrates what you're actually doing without adding any extra stuff or leaving out any code required to repeat the problem.

    Another thing to keep in mind is that literals that start with 0 are considered octal.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    Thinking about it logically the error I get does not make sense because in the class I am testing there is only one method that contains an array. And when testing any other method i.e. an arrayless method it still comes up with the same array index out of bounds exception, surely that makes no sense at all?!

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    Like I've said, without an SSCCE, we can't really help.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    I posted the testable method and the test case, what else do you need?

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    Quote Originally Posted by Hazza View Post
    I posted the testable method and the test case, what else do you need?
    I ran the testable method and didn't get any errors, but I had to change things because you didn't post all of the code that is required to run the program.

    If you read the SSCCE link, you'll see that the code you post should be self-contained: not just snippets like you've posted here, but not hundreds of lines of code either. We should be able to copy and paste the code and get exactly the same results as you do.. otherwise how are we supposed to help you?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    Okay let me try it this way:

    The method of the class (the class is called Date):

    Java Code:
    public static int numberOfDaysInMonth(int month, int year) {
    		int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	    
    	    if (month == 2 && isLeapYear(year)) {
    	        return 29;
    	    } else {
    	        return daysInMonth[month-1];
    	    }
    	}
    The test case:

    Java Code:
    public class TestDate {
            Date nonLeapYearFebruary;
    	Date leapYearFebruary;
    
    @Before
    	public void setup() {
                    nonLeapYearFebruary = new Date(28, 02, 2013);
    		leapYearFebruary = new Date(29, 02, 2012);
    
    @Test
    	public void testNumberOfDaysInNonLeapYearFebruary() {
    		int month = 02;
    		int year = 2013;
    		int result = Date.numberOfDaysInMonth(month, year);
    		assertEquals(28, result);
    	}
    	
    	@Test
    	public void testNumberOfDaysInFebruaryOfALeapYear() {
    		int month = 02;
    		int year = 2012;
    		int result = Date.numberOfDaysInMonth(month, year);
    		assertEquals(29, result);
    	}

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    That code has the same problem. For example, we have no idea what the isLeapYear() method does, or whether it's a part of the problem. We can't copy and paste that code and run it as it's not in a class, nor does it have a main() method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    The isLeapYear() method:

    Java Code:
    public static boolean isLeapYear(int year) {
    	    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    	}
    I am not using a main() method, can you not use JUnit on your version of eclipse?

  14. #14
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    The method in a class with the isLeapYear() method:

    Java Code:
    public class Date {
    
    public static int numberOfDaysInMonth(int month, int year) {
            int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
             
            if (month == 2 && isLeapYear(year)) {
                return 29;
            } else {
                return daysInMonth[month-1];
            }
        }
    
    public static boolean isLeapYear(int year) {
    	    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    	}	
    }

  15. #15
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    Well this is a pretty useless forum...

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    You don't think our inability to lead you to a solution could be the lack of information?

    I mean, you got an exception.
    That exception has a stack trace.
    You don't provide the stack trace, or highlight where it is thrown from.
    You just post a few fragments of code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    What is a stack trace?

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: JUnit test and ArrayIndexOutOfBoundsException

    Your exception will consist of something like:
    Java Code:
    Exception in thread "main" java.lang.RuntimeException: Gone wrong
    	at stuff.Scratch.main(Scratch.java:80)
    So, the exception name, some descrptive text, and the stack trace, in this case one line, but will usually be several.
    That line tells my that line 80 in Scratch.java threw the RuntimeException.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JUnit test nullPointerException
    By danboy4 in forum New To Java
    Replies: 1
    Last Post: 10-10-2012, 07:06 AM
  2. JUnit test - AssertEquals
    By dellacpa in forum New To Java
    Replies: 2
    Last Post: 11-21-2010, 09:19 PM
  3. Replies: 1
    Last Post: 08-03-2009, 05:31 PM
  4. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 05:15 PM
  5. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 01:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •