Studyon Minte9.com
JUnit




Real Example



NearHundred() function: Given an int n, return true if it is within 10 of 100 or 200

When you run the test with correct function (nearHundredNew) the result is Exception:

	%java

		package testexample;

		import org.junit.Test;
		import org.junit.Assert.*;
		import junit.framework.*;

		import java.util.Map;
		import java.util.HashMap;

		public class Testexample {
		    
		    public static void main(String[] args) {
			
			// App logic
			MyClass myClass = new MyClass();
			if (myClass.nearHundred(93)) {
			    System.out.println("The number is valid");
			}
			    // Output: The number is valid
			
			
			/* --- LOOK HERE --- */
			
			// Unit Testing
			new Junittest().testNearHundred();

		    }
		}

		class MyClass {
		    
		    /*
		     * Current version (CORRECT)
		     * 
		     * Given an int n, return valid if it is within 10 of 100 or 200
		     * 
		     */
		    public Boolean nearHundred(int n) {
			if ( (Math.abs(n-100) <= 10) || (Math.abs(n-200) <= 10) ) {
			    return true;
			} else {
			    return false;
			}
		    }
		    
		     /*
		     * Wrong version (INCORRECT)
		     * 
		     * Given an int n, return valid if it is within 10 of 100 or 200
		     * 
		     */
		    public Boolean nearHundredNew(int n) {
			return n-100 <= 10 || n-200 <= 10 ? true : false;
		    }
		}

		class Junittest extends TestCase {

		    @Test
		    public static void testNearHundred() {
			
			Map<Integer, Boolean> numbers = new HashMap<Integer, Boolean>();
			
			numbers.put(93, true);
			numbers.put(90, true);
			numbers.put(99, true);
			numbers.put(100, true);
			numbers.put(101, true);
			numbers.put(109, true);
			numbers.put(110, true);
			numbers.put(111, true);
			numbers.put(193, true);
			numbers.put(199, true);
			numbers.put(200, true);
			numbers.put(201, true);
			numbers.put(209, true);
			numbers.put(210, true);
			
			numbers.put(89, false);
			numbers.put(111, false);
			numbers.put(211, false);
			numbers.put(0, false);
			numbers.put(-100, false);
			numbers.put(10, false);
			
			for(Map.Entry<Integer, Boolean> row:numbers.entrySet()) {
			    
			    //Boolean testResult = new MyClass().nearHundred(row.getKey());
			    Boolean testResult = new MyClass().nearHundredNew(row.getKey());
			    
			    if(row.getValue() == true) {
				
				System.out.println(row.getKey() + " is VALID");             
				assertTrue(testResult);
			    }
			    
			    if(row.getValue() == false) {
				
				System.out.println(row.getKey() + " is INVALID");             
				assertFalse(testResult);
			    }
			}
			
			/*  I)
			    When you run the test with correct function (nearHundred)
			    the result is: 
			
			    0 is INVALID
			    100 is VALID
			    101 is VALID
			    201 is VALID
			    99 is VALID
			    200 is VALID
			    110 is VALID
			    111 is INVALID
			    10 is INVALID
			    199 is VALID
			    109 is VALID
			    193 is VALID
			    93 is VALID
			    -100 is INVALID
			    89 is INVALID
			    209 is VALID
			    210 is VALID
			    211 is INVALID
			    90 is VALID
			 
			    II)
			    When you run the test with correct function (nearHundredNew)
			    the result is Exception:
			    
			    0 is INVALID (not passing the test)
			    
			 */
		    }
		}