Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with JUnit testing

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Help with JUnit testing

    So I am brand spanking new to JUnit testing and code testing in general. I found a helpful guide on YouTube but it only got me so far. I wrote a simple Change Making problem and now I want to do some JUnit testing on it. Here is the method I am attempting to test:

    public <String> List<String> minCoins(int[] coins, int amount){
     
            List list = new ArrayList();
     
            int num;
     
            if(coins.length == 0){
                throw new IllegalArgumentException("Array of size 0 is not allowed.");
            }
     
            for(int i = 0; i < coins.length; i++){
     
                if(coins[i]<=amount){
                    num = amount/coins[i];
     
                    for(int j = 0; j < num; j++){
     
                        list.add(coins[i]);
                    }
     
                    amount -= num*coins[i];
                }
            }
     
            return list;
        }

    First test:

    @Test
        public void testMinCoins1() {
            System.out.println("minCoins");
            int[] coins = {25,10,5,1};
            int amount = 50;
            Lab7 instance = new Lab7();
            List expResult = ??;
            List result = instance.minCoins(coins, amount);
            assertEquals(expResult, result);
        }

    Not sure what to put in the expResult section. I tried putting in (25,25) but it says:

    ')' expected
    <identifier> expected
    incompatible types int cannot be converted to List

    Not sure what's going on there. I would expect since my list is filled with int's that int's would be what is expected on the output. I know the program runs because I created a driver for it and ran it.

    Second thing I want to test is if I have an empty array I want it to throw an exception but I have no idea how to do that.

    Any help would be appreciated!

  2. #2
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Help with JUnit testing

    Okay well I figured out how to test the contents of my list properly if anyone is wondering here is my test case:

    @Test
        public void testMinCoins() {
            System.out.println("minCoins");
            int[] coins = {25,10,5,1};
            int amount = 10;
            Lab7 instance = new Lab7();
            List<Integer> expected = new ArrayList<>();
            expected.add(25);
            List result = instance.minCoins(coins, amount);
            assertEquals(expected, result);
            // TODO review the generated test code and remove the default call to fail.
            fail("The test case is a prototype.");
        }

    So that is good. However, I still don't understand how to test my NullPointerException. I have tried a few different things to no avail. The problem I am having is that I can not seem to find the correct method for testing for my specific IDE which is NetBeans. Either that or the IDE doesn't matter and my JUnit version is the problem. I can't seem to figure out which version of JUnit I am running.

    If someone could show me how to test my NullPointerException that would be great!

Similar Threads

  1. Using Junit testing to test
    By egreenhorn in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 15th, 2014, 11:29 AM
  2. NullPointerException with JUnit Testing
    By 3sbwya in forum Exceptions
    Replies: 3
    Last Post: February 28th, 2014, 07:47 PM
  3. Java automated testing tools for Unit testing
    By rameezraja in forum Member Introductions
    Replies: 2
    Last Post: April 14th, 2012, 08:51 AM
  4. JUnit Testing
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2012, 11:19 AM
  5. JUnit problems
    By degude in forum Java SE APIs
    Replies: 8
    Last Post: September 19th, 2011, 05:54 AM