Total Pageviews

2014/11/07

JUnit Quick Start

Assume I has one utility class, which provide some services, including find employee name, find all employees, etc.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class MyUtils {  
  /**  
   * Checks for this employee.  
   *   
   * @param name  
   *      the name  
   * @return true if find data; false if find nothing  
   */  
  public static Boolean hasThisEmployee(final String name) {  
     Boolean hasData = Boolean.FALSE;  
     List strs = initData();  
     hasData = findData(name, hasData, strs);  
     return hasData;  
   }  
   /**  
    * Find employee.  
    *   
    * @param name  
    *      the name  
    * @return employee name if find; null if not find  
    */  
   public static String findEmployee(String name) {  
     Boolean hasData = Boolean.FALSE;  
     List strs = initData();  
     hasData = findData(name, hasData, strs);  
     String employee = null;  
     if(hasData) {  
       employee = name;  
     }  
     return employee;  
   }  
   /**  
    * Find all employee.  
    *   
    * @return list of String  
    */  
   public static List findAllEmployee(){  
     return initData();  
   }  
   /**  
    * Find employee.   
    *   
    * @param name  
    * @param hasData  
    * @param strs  
    * @return true if find data; false if find nothing  
    */  
   private static Boolean findData(final String name, Boolean hasData, List strs) {  
     Predicate predicate = new Predicate() {  
       @Override  
       public boolean apply(String input) {  
         return name.equalsIgnoreCase(input);  
       }  
     };  
     Iterator iterator = Collections2.filter(strs, predicate).iterator();  
     while (iterator.hasNext()) {  
       if (!Strings.isNullOrEmpty((String) iterator.next())) {  
         hasData = Boolean.TRUE;  
         break;  
       }  
     }  
     return hasData;  
   }  
   /**  
    *   
    * @return initial data  
    */  
   private static List initData() {  
     List strs = new ArrayList<>();  
     strs.add("Albert");  
     strs.add("Mandy");  
     strs.add("Eric");  
     return strs;  
   }  
 }  


If we would like to do unit test, this unit test class will demonstrate how to use assertSame, assertNotNull, and assertArrayEquals. 
All test methods are annotated with @Test

Here is a template for writing JUnit4-style tests:
package com.example.foo;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests for {@link Foo}.
 *
 * @author user@example.com (John Doe)
 */
public class FooTest {

    @Test
    public void thisAlwaysPasses() {

    }

    @Test
    @Ignore
    public void thisIsIgnored() {
    }
}

And we will demonstrate some assert methods, including

  • assertArrayEquals(java.lang.Object[] expecteds, java.lang.Object[] actuals) : 
    • Asserts that two object arrays are equal.
  • assertSame(java.lang.Object expected, java.lang.Object actual) :  
    • Asserts that two objects refer to the same object.
  • assertNotNull(java.lang.Object object) : 
    • Asserts that an object isn't null.
  • assertEquals(long expected, long actual) : 
    • Asserts that two longs are equal.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class FooTest {  
  /**  
   * Test assert same.  
   * The list includes Albert, Mandy, Eric, and Obama,  
   * it should return True if I use "Albert" to search  
   */  
  @Test  
  public void testAssertSame() {  
    assertSame(Boolean.TRUE, MyUtils.hasThisEmployee("Albert"));  
   }  
   /**  
    * Test assert not null.  
    * The list includes Albert, Mandy, Eric, and Obama,  
    * it should return not null if I use "Mandy" to search  
    */  
   @Test  
   public void testAssertNotNull() {  
     assertNotNull(MyUtils.findEmployee("Mandy"));  
   }  
   /**  
    * Test assert array equals.  
    * The list includes Albert, Mandy, Eric, and Obama,  
    * so it should return equal  
    */  
   @Test  
   public void testAssertArrayEquals() {  
     List strs = new ArrayList<>();  
     strs.add("Albert");  
     strs.add("Mandy");  
     strs.add("Eric");  
     assertArrayEquals(strs.toArray(), MyUtils.findAllEmployee().toArray());  
   }  
 } 

If all tests are executed as expected, then you will see success result


If some test does not execute as expected, then you will see fail result

Aggregating tests in suites
If you would like to run multiple test classes at the same time, you can do this way
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 import org.junit.runner.RunWith;  
 import org.junit.runners.Suite;  
 import org.junit.runners.Suite.SuiteClasses;  
 @RunWith(Suite.class)  
 @SuiteClasses({  
   FooTest.class,  
   FooTest2.class  
 })  
 public class TestSuite {  
    // the class remains empty,  
    // used only as a holder for the above annotations  
  }  
 

There are two test case class (FooTest.class, FooTest2.class) in the test suite. In the test result, you can see it will run all test cases in FooTest.class and FooTest2.class


Ignore Test
If for some reason, you just want it ignored, you temporarily disable a test. You just need to add @Ignore annotation
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 public class FooTest2 {  
   @Test  
   @Ignore  
   public void testAssertEqual() {  
     assertEquals(36, MySecondUtils.getAge(1978));  
   }  
   @Test  
   public void testEquals() {  
     assertEquals("Pass", MySecondUtils.getExamResult(88));  
    }  
    @Test  
    public void testAssertThat() {  
      assertThat(MySecondUtils.getExamResult(88).equals("Pass"));  
    }  
  }  
 


See.... testAssertEqual method had been ignored.




Reference
[1] https://github.com/junit-team/junit/wiki

No comments: