Results 1 to 2 of 2
  1. #1
    Vysero is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default JUnit testing questions

    So I am brand spanking new to JUnit testing and code testing in general. I am also fairly new to Java programming 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:

    Java Code:
    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:

    Java Code:
    @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
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,217
    Rep Power
    12

    Default Re: JUnit testing questions

    I have not done JUnit testing in a while but the idea is as follows. You have a result of the method, and the expected result. Say you are multiplying 3 and 5. The expected result if 15. The result is what you compute in your method. If the expected result is not equal to the result, the test fails.

    In your situation, the arguments must be a list (which is why 25, 25 didn't work). You can't test for every case so you need to test for border cases. These might be a null list, an empty list, a maximum size list, etc.

    However, you say you are new to Java so I would forget about JUnit testing until you get more familiar with the Java language. Your use of
    generics is inconsistent throughout your program. And in your minCoins program, you are adding an int to a list but the method is supposed to return a list of type String.

    Regards,
    Jim
    Last edited by jim829; 04-16-2018 at 05:57 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Database junit testing
    By mishy in forum Advanced Java
    Replies: 2
    Last Post: 05-28-2014, 02:52 PM
  2. junit exception testing
    By NoobieCode in forum New To Java
    Replies: 3
    Last Post: 10-10-2013, 10:06 AM
  3. Junit testing, testing list<e> interface
    By mackavelirip in forum New To Java
    Replies: 0
    Last Post: 10-05-2011, 06:08 AM
  4. JUnit testing
    By hedonist in forum Advanced Java
    Replies: 4
    Last Post: 10-04-2009, 12:06 PM
  5. Testing JUnit PDE with Ant
    By keynan in forum Eclipse
    Replies: 0
    Last Post: 02-14-2008, 11:35 PM

Posting Permissions

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